Reputation: 132
I am working on a project using Zend Framework 2 and Doctrine 2.
I have the following process for maintaining the database :
I would like to enrich the generated entities with custom business code : I could simply edit the generated entities but that would mean losing my custom code at each new entity generation and I'm looking for a better workflow.
Basically, this is what I whish to be possible to do :
Generated entity :
namespace My\Base\Entity;
class Cart
{
private $quantity;
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
public function getQuantity()
{
return $this->quantity;
}
}
Enriched entity :
namespace My\Entity;
class Cart extends \My\Base\Entity\Cart
{
public function setQuantity($quantity)
{
$quantity = filter_var(
$quantity,
\FILTER_VALIDATE_INT, array('min_range' => 1)
);
if ($quantity === false) {
throw new \InvalidArgumentException(
'Quantity should be an integer higher than 0'
);
} else {
return parent::setQuantity($quantity);
}
}
}
This is not working because Doctrine expects \My\Entity\Cart to be an entity itself (while its only an extension of the base entity).
Is there any way to implement anything similar to that ?
Upvotes: 0
Views: 1207
Reputation: 1121
You have to mark the base entity as a mappedSuperclass
/** @MappedSuperclass */
class BaseCart
Upvotes: 0
Reputation: 666
In my opinion it depends on what would you like to achieve.
In described code I see something like simple validation.
I extended my Entity class with function getInputFilter(), where I am keeping rules for validation the single record.
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
// [...]
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
Please check following link to see complete code: https://github.com/evolic/zf2-tutorial/blob/v0.4.2/module/Album/src/Album/Entity/Song.php
Other methods, like retrieving all records or single one I put in the model class: (/module/Album/src/Album/Model/Song.php)
Controller class can be found here: (/module/Album/src/Album/Controller/SongController.php)
Whole code can be found at: https://github.com/evolic/zf2-tutorial/blob/v0.4.2/
I am not generating entities basing on database. I create them on my own.
I hope that help you.
Upvotes: 0
Reputation: 10684
Unfortunately, no. The code generation functionality in Doctrine is provided as a "third class citizen", meaning you should not rely on it. It's fine for an initial skeleton generation of your entities, but after that you are supposed to edit them by hand.
Though in most cases, the entity generator is clever enough to figure out the differences in your entities if you modify the schema, and it will add only the correct methods. It will never overwrite your entity completely. It will also never pick up property deletions.
I suggest trying to let the entity generator do it's work, then doing a diff with your version control software of choice, to see if it did the right thing.
Upvotes: 1