在Doctrine 1.2中防止数据库中的重复值

时间:2015-02-01 23:46:10

标签: php doctrine symfony1

我有一个填写表格可以正常工作。最近用户抱怨在数据库中保存了重复的值。表Lts与一对多关系中的table2 Koleks有关

动作

public function executeKolect(sfWebRequest $r){
  $areaId = //code;
  $this->lts = Doctrine_Core::getTable('Lts')->getPaid($areaId, $this->selectedLot);
 if($r->isMethod('post')) {
 //code
 }
foreach($this->lts as $l) {
            $amount = $r->getParameter('amount_received_'.$l['id']);
            $dateOfPayment = $r->getParameter('date_of_payment_'.$l['id'], date('Y-m-d'));
            $startPayment = $l['date_start_of_payment'];//date_of_payment should not less than date_start_of_payment
            $collectionsTable = Doctrine_Core::getTable('Koleks')->getDateCreate($l['id'],$dateOfPayment);
//here i want to make a method to check if the date_of_payment value that i want to insert does not exist yet in the Koleks table related to Lts table
 $koleksTable = Doctrine_Core::getTable('Koleks')->getDateCreate($l['id'],$dateOfPayment);
if ($koleksTable >0) {
                 $this->fieldErrors['date_of_payment_'.$l['id']] = 'exist';  
            }
else {
   //code ..proceed

}      }

现在在学说表

public function getDateCreate($loanId,$datePayment) {
    $q = $this->createQuery('k')
                //->select('k.date_created')
              //  ->from('Koleks k')
                ->innerJoin('k.Lts l')
                ->where('k.l_id=?',$loanId)
                ->andWhere('k.date_created=?',$datePayment);
               //return $q->count();
                $dateCreate = $q->count();
               if ($dateCreate == 1) {
                   $res = $q->fetchAll();
                   return $res;
               }
               else {
                return $dateCreate;
               }
}

如果相同的' date_created'我想要返回错误值已存在于Koleks表中

但是这段代码不起作用..

在我之前的项目中,使用mysqli,我可以轻松地避免'使用此代码重复

//操作

$collectionsTable=$link->getdateCreated($date,$id) 
if $collectionsTable > 0 {
echo "cant proceed"
}
else {
//code to proceed
}

//在myclass.php中

$date = _POST//
function getDateCreate($date) {
    $query  = $this->link->query("SELECT date_created FROM Koleks WHERE date_created = '$date'");
    $rowcount = $query->rowCount();

    if($rowcount == 1) 
    { 
       $result = $query->fetchAll();
       return $result;
    }
    else 
    {
       return $rowcount;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为Doctrine Table中的代码应该是(仔细检查别名)

//[...]            
$koleksTable = Doctrine_Core::getTable('Koleks')->getDateCreate($l['id'],$dateOfPayment);//GET THE QUERY
$koleksTable->execute(); //EXECUTE THE QUERY
if ($koleksTable->count() >0) {
     $this->fieldErrors['date_of_payment_'.$l['id']] = 'exists';
}
else 
{
     //code ..proceed
}

并在你的行动中

target_files = Dir["/some/dir/path/*.rb"]
#=> ["/some/dir/path/foo.rb", "/some/dir/path/bar.rb", "/some/dir/path/baz.rb"]