John Davidson

php - Doctrine Refactor entities

0 comments
Message:


Hello i am using Doctrine ORM, i have major problem. Overall i have 10 tables, each table has it's own address fields(street, city, state, zip). So basically i am trying to refactor db structure, and created one new address table with those fields, old tables will hold address_id as a reference, and i will remove address fields from each table, and migrate data.


Since it's very big project with large code base trying to determine whats the best approach to do this refactoring regarding entities.


Things to concern



  1. find() method

  2. findBy() method

  3. persist

  4. Query Builder


Here it's my userCreditCard entity, it has address object in it, and that object should be replaced with "address_id".


class UserCreditCard extends LoggableEntity implements UserCreditCardInterface
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;

/**
* @var int|null
*
* @ORM\Column(name="user_id", type="integer", nullable=true)
*/
protected $userId;


/**
* @var string
*
* @ORM\Column(name="cc_number", type="string", length=255, nullable=false)
*/
protected $ccNumber;

/**
* @var string
*
* @ORM\Column(name="cc_name", type="string", length=255, nullable=false)
*/
protected $ccName = '';

/**
* @var \DateTime
*
* @ORM\Column(name="cc_date", type="date", nullable=false)
*/
protected $ccDate;

/**
* @var bool
*
* @ORM\Column(name="bill_addr_type", type="boolean", nullable=false)
*/
protected $billAddrType = false;

/**
* @var UserBillingAddress
* @ORM\Embedded(class = "\Cae\User\Dal\Entity\Embeddable\UserBillingAddress", columnPrefix=false)
*/
protected $address;

Here is address object.


/** @ORM\Embeddable */
class UserBillingAddress extends AbstractAddressEmbeddable
{
/**
* @var string
*
* @ORM\Column(name="bill_street_address", type="string", length=255, nullable=false)
*/
protected $address = '';

/**
* @var string
*
* @ORM\Column(name="bill_street_address2", type="string", length=255, nullable=false)
*/
protected $address2 = '';

/**
* @var string
*
* @ORM\Column(name="bill_city", type="string", length=255, nullable=false)
*/
protected $city = '';

My question would be since it's very large project is it possible to some how override with entity listeners or with magic setter so for example when someone do


$this->userCreditCardRepository->find($id);

pull address data from new Address table (i can't do joins since address table lives in another module, so i have to use public services).


Maybe to override find, findBy and other methods in AbstractRepository, but i am not sure what the best approach? Any idea is appreciated.



My Answer:
a reference to the new address table:

php

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="user_credit_card")
*/
class UserCreditCard
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity="Address")
* @ORM\JoinColumn(name="address_id", referencedColumnName="id")
*/
private $address;

// Other properties and methods...

public function getAddress(): Address
{
return $this->address;
}

public function setAddress(Address $address): void
{
$this->address = $address;
}
}


In this example, I have replaced the address object in the `UserCreditCard` entity with a reference to the new `Address` entity. You will need to create the `Address` entity with the address fields (street, city, state, zip) and update the mapping in the `UserCreditCard` entity to point to the new `Address` entity.

After making these changes, you will need to update your database schema to reflect the new structure. You can use Doctrine migrations to generate and execute the necessary SQL queries to update your database schema.

Once the database schema has been updated, you can migrate the data from the old tables to the new `Address` table. You can do this using Doctrine's query builder to select the data from the old tables and insert it into the new `Address` table.

Overall, the key steps in this refactoring process are:
1. Create a new `Address` entity with the address fields.
2. Update the existing entities to reference the new `Address` entity.
3. Update the database schema using Doctrine migrations.
4. Migrate the data from the old tables to the new `Address` table using Doctrine query builder.

By following these steps, you can refactor your database structure to use a separate `Address` table and update your entities accordingly.

Rate this post

3 of 5 based on 3363 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us