http://bakery.cakephp.org/articles/view/bindable-behavior-control-your-model-bindings
There's already a behavior written to do just this. It's excellent.
On Apr 26, 12:07 pm, senser <[EMAIL PROTECTED]> wrote:
> H!i
>
> I'm trying to write a simple behavior for binding models but may be I
> do something wrong cause using behavior causes errors in sql queries
> generated by CakePHP.
>
> Here is detailed info (a snippet from whole application):
> One model "Offer" (primary used) which *hasMany* "Category" & "Type".
> For every one of these three models I have a three models "OfferI18n",
> "CategoryI18n" & "TypeI18n" where I want to store translated details
> for the primary models (yes, I know that there is built-in translate
> behavior but it's not suitable for my purposes).
> So I try to bind these *I18n models to "primary" models in behavior.
> Here is the code of behavior:
> ############################
> class LanguageBehavior extends ModelBehavior{
> function setup(&$model, $config) {
> $db =& ConnectionManager::getDataSource($model->useDbConfig);
> if (!$db->connected) {
> trigger_error('Datasource '.$model->useDbConfig.' for
> LanguageBehavior of model '.$model->alias.' is not connected',
> E_USER_ERROR);
> return false;
> }
>
> $lang_model=array($model->name.'I18n'=>array('foreignKey'=>
> $config['key']));
> $model->bindModel(array('hasMany'=>$lang_model));
> }}
>
> ############################
>
> And there is how I'm trying to use the behavior in models:
> ############################
> class Offer extends AppModel{
> var $actsAs=array('Language'=>array('key'=>'offer_id'));
> .
> .
> .
>
> }
>
> class Category extends AppModel{
> var $actsAs=array('Language'=>array('key'=>'category_id'));
> .
> .
> .
>
> }
>
> class Type extends AppModel{
> var $actsAs=array('Language'=>array('key'=>'type_id'));
> .
> .
> .}
>
> ############################
>
> So here is the problem - in Offer controller if I try to fetch data,
> doing for example "$this->Offer->findAll()" and everything is OK
> (models are binded and data is fetched). But after receiving all
> offers I try to receive some Category (""$this->Offer->Category-
>
> >findAll()"") and I receive error like this:
>
> "Query: SELECT `Category18n`.`id`, `Category18n`.`category_id`,
> `CategoryI18n`.`locale`, `Category18n`.`content`, `CategoryI18n`. FROM
> `category_i18ns` AS `CategoryI18n` WHERE locale="bul" AND
> Category18n`. IN (1, 2, 3, 4)"
>
> As you can see - the query fails because this: "Category18n`. IN (1,
> 2, 3, 4)" the field of the table CategoryI18n is missing.
>
> Very strange, but if I don't execute any query on Offer before,
> Categories are fetched OK, but if I run any query on Offer and other
> models (Category & Type) broke.
>
> I suppose there is something wrong in my behavior but I don't know
> what.
>
> Please help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---