O
O
Oreolek2011-06-06 11:25:45
ORM
Oreolek, 2011-06-06 11:25:45

ORM in Kohana 3.1?

The scheme is rather non-trivial, and I cannot translate it into ORM. Writing through the Query Builder will not work, since you will have to shovel hundreds of lines with ORM objects.
model.png
The Owner field refers to the Auth module's users table, if you're interested.
The problem is in three fields of the events table that refer to the PRIMARY KEY of the days table. How to describe them? Constantly pops up an error when trying to write.
Here are the models without the damn margins:

class Model_Event extends ORM {<br/>
 protected $_table_columns = array(<br/>
 'id' =&gt; array('data_type' =&gt; 'int', 'is_nullable' =&gt; FALSE),<br/>
 'owner' =&gt; array('data_type' =&gt; 'int', 'is_nullable' =&gt; FALSE),<br/>
 'name' =&gt; array('data_type' =&gt; 'tinytext', 'is_nullable' =&gt; FALSE),<br/>
 'starting_time' =&gt; array('data_type' =&gt; 'time', 'is_nullable' =&gt; FALSE),<br/>
 'ending_time' =&gt; array('data_type' =&gt; 'time', 'is_nullable' =&gt; FALSE),<br/>
 'place' =&gt; array('data_type' =&gt; 'tinytext', 'is_nullable' =&gt; FALSE),<br/>
 'person_in_charge' =&gt; array('data_type' =&gt; 'tinytext'),<br/>
 'seats' =&gt; array('data_type' =&gt; 'int'),<br/>
 'info' =&gt; array('data_type' =&gt; 'mediumtext'),<br/>
 'program' =&gt; array('data_type' =&gt; 'mediumblob'),<br/>
 'verified' =&gt; array('data_type' =&gt; 'bool'),<br/>
 );<br/>
 protected $_has_many = array(<br/>
 'owner' =&gt; array(<br/>
 'model' =&gt; 'user',<br/>
 'foreign_key' =&gt; 'owner')<br/>
 );<br/>
};

class Model_Day extends ORM {<br/>
 protected $_table_columns = array(<br/>
 'day' =&gt; array('data_type' =&gt; 'date', 'is_nullable' =&gt; FALSE),<br/>
 'max_events' =&gt; array('data_type' =&gt; 'int', 'is_nullable' =&gt; FALSE, 'default' =&gt; 10),<br/>
 );<br/>
 protected $_primary_key = 'day';<br/>
};<br/>

Here is the simplified entry code:
$event-&gt;values(Arr::extract($_POST, array('name','starting_time','ending_time','place','person_in_charge','seats','info'), NULL));<br/>
$event-&gt;owner = Auth::instance()-&gt;get_user()-&gt;id;<br/>
 $starting_day = new Model_Day;<br/>
 $ending_day = new Model_Day;<br/>
 $starting_day -&gt; day = Request::current()-&gt;post('starting_day');<br/>
 $ending_day -&gt; day = Request::current()-&gt;post('ending_day');<br/>
 $opened_until = new Model_Day;<br/>
 $opened_until -&gt; day = Request::current()-&gt;post('opened_until');<br/>
 if (!($starting_day-&gt;find())) $starting_day -&gt; create();<br/>
 if (!($ending_day-&gt;find())) $ending_day -&gt; create();<br/>
 if (!($opened_until-&gt;find())) $opened_until -&gt; create();<br/>
 $event-&gt;add('starting_day',$starting_day);<br/>
 $event-&gt;add('ending_day',$ending_day);<br/>
 $event-&gt;add('opened_until', $opened_until);<br/>
 $event-&gt;create();<br/>

I draw your attention to the fact that the version of Kohana is 3.1.
I tried to describe through belongs_to, has_many, combined both approaches - it does not work. Either swears at FOREIGN KEY, or at the missing starting_day fields and others like it, or at an empty table name. Plugging occurs exactly on the line $event-&gt;add('starting_day',$starting_day);, which is typical.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2011-06-07
@Oreolek

The add() method only works for ManyToMany relationships. For singly related fields (HasOne, BelongsTo) you should use something like $event->starting_day = $starting_day

N
NeX, 2011-06-06
@NeX

public function add($alias, $far_keys)
the alias name is specified, not the field. Show the code of models (interested belongs_to and has_many) day and event

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question