开发者

Checking for duplicate values in a Model class

开发者 https://www.devze.com 2023-02-04 14:16 出处:网络
I have a model, Entity, and I built an EntityMapper and an Entity class (I\'m just learning to use Zend Framework and following the tutorials). The Entity class has a setName method, and what I want i

I have a model, Entity, and I built an EntityMapper and an Entity class (I'm just learning to use Zend Framework and following the tutorials). The Entity class has a setName method, and what I want it to do is check if there is another "entity" in the DB with the same name, and in that case throw an exception or something.

So, If I understand correctly, DB calls should only be in the Mapper class. So, inside setName, should I do something like:

$entity = new Application_Model_EntityMapp开发者_运维问答er();
if ($entity->checkDuplicateName($name, $this->_id))
  $this->_name = $name;
else
  throw new Exception(...);
return $this;

and put the code that actually does the query in a new method in the Mapper class? (the query should, of course, be different if the "entity" is new or if it has an id already, but that's not the point of my question).

I know I could do this in a couple of ways, but my goal is to adjust as much as possible to the conventions of the framework.


Since saving is a duty of the Mapperobject I'd add the validation to the save routine of your mapper class. I didn't understand what respective duties your different classes have, so I'll explain mine:

-Application_Model_Entity is a pure struct for data, this class has no dependencies
-Application_Model_EntityMapper posses the right to speak with the dbrms, will transform Entities in Records and vice versa. It "owns" the ActiveRecord (DbTable) class -Application_Model_DbTable_Entity is the ActiveRecord class, it extends from Zend_DbTable_Abstract and is capable of doing queries against the DB, it is only used by the Mapper.

$entity = new Application_Model_Entity();
$entity->setName('something which already exists');

$mapper = new Application_Model_EntityMapper();
$mapper->save($entity); // throws Exception

// works with: 
class Application_Model_EntityMapper
{
    /** @var Application_Model_DbTable_Entity */
    private $dbTable;

    ...

    public function save(Application_Model_Entity $entity)
    {
        $doValidation = ! $entity->getId(); // no id means not in db yet
        if ( $doValidation )
        {
            $hasDuplicatesValidator = new Zend_Validate_Db_RecordExists(
                'table' => 'entity',
                'field' => 'name'
            );
            $hasDuplicates = $hasDuplicatesValidator->isValid($entity->getName());
            if ( $hasDuplicates )
            {
                throw new Exception('There is already a record in the db with this name!');
            }
        }
        // go on and save
        $this->dbTable->save($entity);
    }
}

I hope the code explains itself. This is the most 'zendish' way i can find, hope this helps you on your way to the zf-community :)

Link to manual for Zend_Validate_*


I found that doing that check in setName causes it to run the query every time it loads a record from the db (not good), so I moved the call to checkDuplicateName to the save method of the Mapper class. (checkDuplicateName is also on the Mapper class, as a private method now)

I would still love to know if this is the standard way of doing this kind of stuff in Zend Framework.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号