protected $_has_many = array('somethings' => array('model' => 'User_Something')); $user = Model_User::find_by_username_and_password('ben', 'password'); $user->somethings->find_all()
// app/modules/news/classes/model/newsletter.php namespace News; class Model_Newsletter extends \ActiveRecord\Model { protected $belongs_to = array('language'); } // app/modules/news/classes/model/language.php namespace News; class Model_Language extends \ActiveRecord\Model { protected $has_many = array('newsletters'); } // app/modules/news/classes/controller/newsletter.php namespace News; class Controller_Newsletter extends \Controller_Template { function action_router($method, $params) { $options = array('include' => array('language')); $newsletter = Model_Newsletter::find_first_by_newsletter_code($method, $options); echo $newsletter->language->language_code; } } // or class Controller_Newsletter extends \Controller_Template { function action_router($method, $params) { $newsletter = Model_Newsletter::find_first_by_newsletter_code($method); echo $newsletter->language->language_code; } }
// app/classes/model/language.php class Model_Language extends ActiveRecord\Model { protected $has_many = array('newsletters'); }
// app/modules/news/classes/controller/newsletter.php namespace News; class Controller_Newsletter extends \Controller_Template { function action_router($method, $params) { $options = array( 'include' => array('language'), // not needed if $belongs_to is set in Model 'where' => array( array('languages.language_code', '=', 'nl') ) ); $newsletter = Model_Newsletter::find_first_by_newsletter_code($method, $options); echo $newsletter->language->language_code; } }
protected $_belongs_to = array('[alias name]' => array('model' => '[model name]', 'foreign_key' => '[column]'));
order
order_item
protected $_has_many = array('items' => array('model' => 'Order_Item'));
protected $has_many = array('sales_flat_invoice_comment', 'sales_flat_invoice_item');
$invoice = Model_Sales_Flat_Invoice:find_by_name('Ben Corlett');That is fine Now, let's find a comment:
$comment_text = $invoice->sales_flat_invoice_comment->text;
1.ErrorException [ Warning ]: call_user_func_array() expects parameter 1 to be a valid callback, class 'Model_SalesFlatInvoiceComment' not found - It's camel-casing when looking for models.
2.The property name get's quite lengthy.
3.Even if it did find it, it would be looking for a foreign key that is Sales_Flat_Invoice_id (as we can't specify the foreign key for this relationship) Let's say I could setup the assocations by doing the following:
protected $has_many = array('comments' => array('model' => 'Sales_Flat_Invoice_Item', 'foreign_key' => 'invoice_id');
php oil package install phpactiverecordOur own AR will probably get a partial or even full rewrite. I've done some work not yet committed already to make our AR more flexible. I can say AR will change, I can't say yet how much but we'll probably keep it mostly backwards compatible.
foreach ($this->{$type} as $assoc) { /* handle association sent in as array with options */ if (is_array($assoc)) { $key = key($assoc); $this->{$key} = new $class_name($this, $key, current($assoc)); } else { $this->{$assoc} = new $class_name($this, $assoc); } }
protected $has_many = array('test' => 'Model_Test');
protected $has_many = array('test' => array('model' => 'Model_Test'));
It looks like you're new here. If you want to get involved, click one of these buttons!