私はCIモデルのために多対多のdbコールを作成しようとしていますが、これを行うにはモデルの中からモデルを呼び出しています...もっと良い方法があれば、すべての耳!モデル内のモデルの変更がCodeIgniterの結果
Models:
- events
- children
- eventChildren
彼らは基本的なCRUD操作を実行します。
は、私は三つのモデルを持っています。私のevents
モデルではwithChildren
パラメータを受け入れるGetEvents
メソッドがあります。 withChildren
パラメータが渡された場合、イベントに関連付けられた子を取得します。
eventChildren
モデルをevents
モデルの中から呼び出すと、その結果が面白くなります。私はeventChildren
モデルの中から$this->db->get('eventChildren');
コールに絞った。この呼び出しが突然行われたとき、私は単一のイベントではなく、複数のイベントを返します。
コード:
if(isset($options['eventId']) && isset($options['withChildren']))
{
// Get ID's of children that are tied to the event.
$this->load->model('eventChildren_model', 'eventChildren');
$this->load->model('children_model', 'children');
$eventChildren = $this->eventChildren->GetEventChildren(array('eventId' => $options['eventId']));
// Loop over those ID's and get the children from the children table
$children = array();
foreach($eventChildren as $group)
{
$child = $this->children->GetChildren(array('childId' => $group->childId));
array_push($children, $child);
}
echo "<pre>";
print_r($children);
echo "</pre>";
}
私はこのコード行をコメントアウトした場合、私は、単一のイベントを返します。このコード行のコメントを外すと、すべてのイベントが返されます。
$eventChildren = $this->eventChildren->GetEventChildren(array('eventId' => $options['eventId']));
これは$this->db->get('eventChildren');
コールに関連しています。その呼び出しが行われると、イベントは変な動作をします。外部モデル呼び出しを行うことなく、
例戻り:
Array
(
[0] => stdClass Object
(
[eventId] => 2
...
)
)
例戻りは、外部モデルを呼び出すとき:すべてのコードの
Array
(
[0] => stdClass Object
(
[childId] => 8
...
)
[1] => stdClass Object
(
[childId] => 10
...
)
)
Array
(
[0] => stdClass Object
(
[eventId] => 1
...
)
[1] => stdClass Object
(
[eventId] => 2
...
)
[2] => stdClass Object
(
[eventId] => 3
...
)
)
申し訳ありません。私は最終的な目標はイベントに子供を追加するMany to Manyオブジェクトを作成して、私の見解の中からこれを簡単に表示できるようにすることです。私がここで読んだことのすべては、モデル内からモデルをロードする習慣を落胆させましたが、私はそれを行うより良い方法はありません。 DataMapperとDoctrineの両方を試してみたところ、正常に動作することができませんでした。
ご協力いただければ幸いです。
イベントモデル(GetEvents):(注:_p()<pre>
タグでラップだけprint_r()
を出力するヘルパー関数である)
function GetEvents($options = array())
{
// default values
$options = _default(array('sortDirection' => 'asc'), $options);
// Add where clauses to query
$qualificationArray = array('eventId', 'eventStatus');
foreach($qualificationArray as $qualifier)
{
if(isset($options[$qualifier])) $this->db->where($qualifier, $options[$qualifier]);
}
// If limit/offset are declared (usually for pagination) then we need to take them into account
if(isset($options['limit']) && isset($options['offset'])) $this->db->limit($options['limit'], $options['offset']);
else if(isset($options['limit'])) $this->db->limit($options['limit']);
// sort
if(isset($options['sortBy'])) $this->db->order_by($options['sortBy'], $options['sortDirection']);
// add children to the event
if(isset($options['eventId']) && isset($options['withChildren']))
{
// Get ID's of children that are tied to the event.
$this->load->model('eventChildren_model', 'eventChildren');
$this->load->model('children_model', 'children');
$eventChildren = $this->eventChildren->GetEventChildren(array('eventId' => $options['eventId']));
// Loop over those ID's and get the children from the children table
$children = array();
foreach($eventChildren as $group)
{
$child = $this->children->GetChildren(array('childId' => $group->childId));
array_push($children, $child);
}
echo "Children:\n";
_p($children);
}
$query = $this->db->get('events');
if($query->num_rows() == 0) return false;
_p($query->result());
if(isset($options['eventId']))
{
// If we know that we're returning a singular record, then let's just return the object
return $query->row(0);
}
else
{
// If we could be returning any number of records then we'll need to do so as an array of objects
return $query->result();
}
}
eventChildrenモデル(GetEventChildren法)
function GetEventChildren($options = array())
{
// default values
$options = _default(array('sortDirection' => 'asc'), $options);
// Add where clauses to query
$qualificationArray = array('eventChildrenId', 'eventId', 'childId');
foreach($qualificationArray as $qualifier)
{
if(isset($options[$qualifier])) $this->db->where($qualifier, $options[$qualifier]);
}
// If limit/offset are declared (usually for pagination) then we need to take them into account
if(isset($options['limit']) && isset($options['offset'])) $this->db->limit($options['limit'], $options['offset']);
else if(isset($options['limit'])) $this->db->limit($options['limit']);
// sort
if(isset($options['sortBy'])) $this->db->order_by($options['sortBy'], $options['sortDirection']);
$query = $this->db->get('eventChildren');
if($query->num_rows() == 0) return false;
if(isset($options['eventChildrenId']))
{
// If we know that we're returning a singular record, then let's just return the object
return $query->row(0);
}
else
{
// If we could be returning any number of records then we'll need to do so as an array of objects
return $query->result();
}
}
概要
私の問題はevent_model
ファイルで、最初の呼び出しを終了する前に外部モデルを呼び出していたので、メインのDB呼び出しを上書きしていました。私はそのコードを$query = $this->db->get('events');
の直下に移動しなければならず、すべてが機能しました。
@landonsにこれを手伝ってくれてありがとう、ありがとう。
私は、短い質問を保つために、モデル全体を掲示避けるしようとしていたが、それは私が次の例を使用しているとされているhttp://heybigname.com/2009/08/28/how-to-write-a -better-model-in-code-igniter /私は2つの '$ this-> db-> get()'呼び出しを持っていますが、それらはそれぞれのモデルにあります。両方のモデルで質問を更新します。 – Seth
私はモデルを追加しました。 – Seth
近づいています。さて、あなたはどんなパラメータを渡していますか、どの出力を期待していますか? – landons