Exemple #1
0
 /**
  * Get the primary key as array (key => value)
  *
  * @param CModel $model
  * @return array
  */
 public function getPrimaryKey($model)
 {
     $result = array();
     if ($model instanceof CActiveRecord) {
         $pkValue = $model->primaryKey;
         if (!empty($pkValue)) {
             $pkName = $model->primaryKey();
             if (empty($pkName)) {
                 $pkName = $model->tableSchema->primaryKey;
             }
             $result = is_array($pkValue) ? $pkValue : array($pkName => $pkValue);
         }
     } else {
         // when working with EMongoDocument
         if (method_exists($model, 'primaryKey')) {
             $pkName = $model->primaryKey();
             $pkValue = $model->{$pkName};
             if (empty($pkValue)) {
                 $result = array($pkName => $pkValue);
             }
         }
     }
     return $result;
 }
 /**
  * Get the primary key as array (key => value).
  * A pk is needed to determine which records to delete.
  * If no pk is available it works too, but the deleteditems on validate is always empty and each record isnew.
  * Detect the pk from CActiveRecord and EMongoDocument.
  * If working with formmodels a custom callbackMethod mmfPrimaryKey of the model will be called if defined.
  *
  * @param CModel $model
  * @return array
  */
 public function getPrimaryKey($model)
 {
     $result = array();
     if ($model instanceof CActiveRecord) {
         $pkValue = $model->primaryKey;
         if (!empty($pkValue)) {
             $pkName = $model->primaryKey();
             if (empty($pkName)) {
                 $pkName = $model->tableSchema->primaryKey;
             }
             $result = is_array($pkValue) ? $pkValue : array($pkName => $pkValue);
         }
     } elseif (method_exists($model, 'primaryKey')) {
         $pkName = $model->primaryKey();
         if (is_array($pkName)) {
             $result = array();
             foreach ($pkName as $pkN) {
                 $result[$pkN] = $model->{$pkN};
             }
         } else {
             $pkValue = $model->{$pkName};
             if (empty($pkValue)) {
                 $result = array($pkName => $pkValue);
             }
         }
     } elseif (method_exists($model, 'mmfPrimaryKey')) {
         $result = $model->mmfPrimaryKey();
     }
     return $result;
 }