私は CodeIgniter を使用して php Web アプリケーションを構築しています。また、適切な OO プラクティスを使用しようとしていますが、これには多くの流派があるようです。特に、MySQL テーブルとやり取りするためのクラス biography_model があります。このデータ モデルには、テーブル内の列を表すいくつかのクラス プロパティがありますが、 などのテーブルにないプロパティもいくつかあります$image_url
。クラス コンストラクター関数は、オプションのレコード ID パラメーターを受け取り、テーブルからそのレコードをフェッチし、get_biography()
メソッドを呼び出して、テーブルにないプロパティを含むすべてのオブジェクト プロパティを設定し$image_url
ます。このようにして、コントローラーで新しい biography_model オブジェクトをインスタンス化し、すべての有用なプロパティをすぐに使用できるようにすることができます。$bio = new biography_model($id);
しかし、テーブルからレコードの複数行の結果セットを返す場合の最善のアプローチは何でしょうか? レコードごとに、 も設定する必要があり$image_url
ます。テーブル内のレコードのリストをクエリし、各 ID を新しい biography_model($id) オブジェクトに渡すことで、コントローラーでこれを行うことができます。しかし、コントローラーがモデルをバイパスしてデータベースに直接クエリを実行している状況が発生します。
代わりに、biography_model 内から biography_model オブジェクトの配列を返すことにしました。
例:
class Biography_model extends Model
{
/**
* This model manages biography information in the 'biography_content' table.
* If a biography ID is passed in when instantiating a new object,
* then all class properties are set.
*/
protected $id;
protected $person_name;
protected $title;
protected $image_file_name;
protected $image_url;
protected $biography_text;
protected $active;
/**
* Constructor
*
* If an id is supplied when instantiating a new object, then
* all class variables are set for the record.
*/
public function __construct($person_id = NULL)
{
parent::Model();
if(isset($person_id))
{
$this->set_property('id',$person_id);
$this->get_biography();
}
}
/**
* Sets supplied property with supplied value.
*/
public function set_property($property, $value)
{
// Set image path if $value is the file name
if($property == 'image_file_name')
{
$this->set_property('image_url',$this->get_bio_img_url($value));
}
$this->$property = $value;
}
/**
* Gets requested property value.
*/
public function get_property($property)
{
return $this->$property;
}
/**
* Returns the biography thumbnail image URL
*/
public function get_bio_img_url($image_name)
{
return $this->config->item('parent_url').'assets/img/biography/'.$image_name;
}
/**
* Get one or more biography entries
*/
public function get_biography()
{
// If the ID is set then set model properties.
if($this->get_property('id'))
{
$this->db->where('id',$this->get_property('id'));
$query = $this->db->get('biography_content');
if($query->num_rows() == 1)
{
foreach($query->row() as $key => $value)
{
$this->set_property($key, $value);
}
}
}
// Otherwise return result set of all biographies
else
{
// Get the list of record ID's
$this->db->select('id');
$query = $this->db->get('biography_content');
if ($query->num_rows() > 0)
{
// New array to return result set
$biography_list = array();
// For each record, return a new biography_model object
foreach($query->result() as $value)
{
$biography_list[] = new biography_model($value->id);
}
}
return $biography_list;
}
}
}
// End of Biography_model Class
できます。しかし、それは合理的なアプローチですか?他にもっと受け入れられている方法はありますか?データベースに 2 回クエリを実行していることはよくわかっていますが、これを処理するより良い方法がわかりませんでした。すべての提案を歓迎します!
ありがとう、ウルフ