いつものように@drew010は正しいです。これらの関数をマッパーに入れます。正直なところ、これらはすべて抽象マッパーまたは基本マッパーに配置できるため、すべてのマッパーで機能を利用できます。
マッパーで関数 recordExists() を簡単に作成し、テーブルと列を引数として渡すだけです。
//you can either pass in the table name or have it be a property
public function recordExists($table = null, $column) {
//The table syntax may change depending on the scope
$table = $this->_tableName;
$exists = new Zend_Validate_Db_RecordExists(array(
'table' => $table,
'field' => $column
)):
return $exists;
}
findBy() メソッドの場合、メソッドに 3 つの変数を渡し、fetchAll() を使用してオブジェクトの配列を返します。そのようにして、列が 1 行または 50 行を返す場合、出力を同じ方法で処理します。
/**
* findByColumn() returns an array of rows selected
* by column name and column value.
* Optional orderBy value, pass $order as string ie 'id ASC'.
*
* @param string $column
* @param string $value
* @param string $order
* @return array returns an array of objects
*/
public function findByColumn($column, $value, $order = NULL) {
$select = $this->_getGateway()->select();
$select->where("$column = ?", $value);
if (!is_null($order)) {
$select->order($order);
}
$result = $this->_getGateway()->fetchAll($select);
$entities = array();
foreach ($result as $row) {
//create objects
$entity = $this->createEntity($row);
$entities[] = $entity;
}
return $entities;
}
最後の質問については、行をフェッチしてからスイッチを保存するだけでレコードをアクティブにすることができます。レコードをアクティブにするために、何らかのタイプのフラグまたはブール値を設定していると思います。レコードを保存するために使用するメソッドでは、基本の save() メソッドが常に機能するとは限らないことがわかったので、これはおそらく具体的な UserMapper に入れる必要があります。
//you'll set the activate switch when the user object is instantiated
public function activate(Application_Model_User $user) {
if (!is_null($user->id)) {
$select = $this->_getGateway()->select();
$select->where('id = ?', $user->id);
$row = $this->_getGateway()->fetchRow($select);
$row->activate = $user->activate;
//This is the cool thing about save(), you can change as many or few columns as you need.
$row->save()
return $row;
} else {
//handle error, as we only want to be able to change an existing user.
}
}
これは必要以上かもしれませんが、それでも役立つことを願っています。