开发者

Create a token for each record using Code Igniter and Doctrine ORM

开发者 https://www.devze.com 2023-01-25 20:38 出处:网络
I have a model setup using Doctrine. I would like to generate a token for each record, so I think I need to overload Doctrine\'s save() function with something like this that I found using Symfony:

I have a model setup using Doctrine. I would like to generate a token for each record, so I think I need to overload Doctrine's save() function with something like this that I found using Symfony:

class JobeetAffiliate extends BaseJobeetAffiliate
{
  public function save(Doctrine_Connection $conn = null)
  {
    if (!$this->getToken())
    {
      $this->setTok开发者_运维问答en(sha1($this->getEmail().rand(11111, 99999)));
    }

    return parent::save($conn);
  }

  // ...
} 

My current model looks like this:

<?php
class Photo extends Doctrine_Record {

    public function setTableDefinition() {
        $this->hasColumn('photo_path', 'string', 255, array('unique' => 'true'));
        $this->hasColumn('count', 'integer', 4, array('unsigned' => 'true'));
        $this->hasColumn('is_count', 'integer', 4, array('unsigned' => 'true'));
        $this->hasColumn('region_id', 'integer', 4);
        $this->hasColumn('region_id', 'integer', 4);
        $this->hasColumn('token', 'string', 255);
    }

    public function setUp() {
        $this->actAs('Timestampable');
        $this->hasOne('Region', array(
            'local' => 'region_id',
            'foreign' => 'id'
        ));     
    }   

}

Any help with generating the token field in the model would be greatly appreciated, as of yet I can't figure out how to overload save() in that model appropriately.


You should look into Doctrine's Record Hooks and it's preSave() function. So you only need to work inside your model:

public function setTableDefinition() {
    $this->hasColumn('photo_path', 'string', 255, array('unique' => 'true'));
    $this->hasColumn('count', 'integer', 4, array('unsigned' => 'true'));
    $this->hasColumn('is_count', 'integer', 4, array('unsigned' => 'true'));
    $this->hasColumn('region_id', 'integer', 4);
    $this->hasColumn('region_id', 'integer', 4);
    $this->hasColumn('token', 'string', 255);
}

public function setUp() {
    $this->actAs('Timestampable');
    $this->hasOne('Region', array(
        'local' => 'region_id',
        'foreign' => 'id'
    ));     
}   

public funcion preSave($event) {
    if( ! $this->token) {
        $this->setToken(sha1($this->getEmail().rand(11111, 99999)));
     }
}


Can't you just add the save() method in your Photo class?

0

精彩评论

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