Re: Overloading AppModel constructor
On Mar 16, 2:42 pm, AD7six wrote: > On Mar 16, 8:12 pm,yaman666 wrote: > > Show what you've got and why, and someone will probably point you in > the right direction. probably: do it with a behavior and var $actsAs = > array('Foo'); in your app model. I have not looked into $actsAs, will research it as soon as I can. Basically models have a custom xml component to store highly customizable data without continuously changing table structure. For example a model table has only "id", "xml" and "created_at" fields. AppModel has a DOMDocument property. On model load it would load xml into dom document. On model save it would save dom document into xml field. And any customizable data can be accessed via xpath through the dom document. class AppModel extends Model { public $dom; public function __construct($id = false, $table = null, $ds = null) { parent::__construct($id, $table, $ds); $this->dom = new DOMDocument; if (isset($this->data[get_class($this)]["xml"])) { $this->dom->LoadXML($this->data[get_class($this)]["xml"]); } } public function beforeSave() { if (isset($this->data[get_class($this)]["xml"])) { $this->data[get_class($this)]["xml"] = $this->dom->SaveXML (); } return true; } } If you can offer another way to do it I'd be really interested to know. Thanks! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Overloading AppModel constructor
We're in the same boat, I would also like to hear an explanation as to "why" we're not supposed to "do it". But the developer is mostly right, bug tracking is not a support forum, so that's the reason why I went and posted on google groups, hoping may be one of them will actually explain the reason. :-) On Mar 16, 2:21 pm, Alfredo Quiroga-Villamil wrote: > I am not sure if there is an alternative; but to me overlading the > constructor sounds perfectly fine. I would be interested in hearing > reasons why not to. I did see the comment posted in the bug and I > personally disagree with the comment: > > "2) Do not override model constructors." > > I would think there is a good reason why not to do that since that was > the response to your issue. It would be interesting to get more > details and not simply say "Don't do it". --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Overloading AppModel constructor
On Mar 16, 8:12 pm, yaman666 wrote: > Well, that's exactly what I have. However, how to populate that > property from database automatically during model creation without > overloading __construct() is what the question is. Show what you've got and why, and someone will probably point you in the right direction. probably: do it with a behavior and var $actsAs = array('Foo'); in your app model. > > P.S. I actually figured out that the reason for exceptions was missing > parameters in constructor, so now it works, but still interested to > see if there's an alternative solution. Quite a big oversight ;) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Overloading AppModel constructor
I am not sure if there is an alternative; but to me overlading the constructor sounds perfectly fine. I would be interested in hearing reasons why not to. I did see the comment posted in the bug and I personally disagree with the comment: "2) Do not override model constructors." I would think there is a good reason why not to do that since that was the response to your issue. It would be interesting to get more details and not simply say "Don't do it". I am glad you figured it out. Regards, Alfredo On Mon, Mar 16, 2009 at 3:12 PM, yaman666 wrote: > > Well, that's exactly what I have. However, how to populate that > property from database automatically during model creation without > overloading __construct() is what the question is. > > P.S. I actually figured out that the reason for exceptions was missing > parameters in constructor, so now it works, but still interested to > see if there's an alternative solution. > > On Mar 16, 1:50 pm, Alfredo Quiroga-Villamil > wrote: >> I haven't tried this; but try it and see what happens. >> >> 1- Create app/models/app_model.php >> 2- In there you can have >> >> classAppModelextends Model { >> >> protected $sYourPropery = 'property'; >> >> } // End ofAppModel{} >> >> All models that extendAppModelshould now have access to >> $sYourProperty. In one of your models for example try the following: >> >> public function testing() { >> return $this->sYourProperty; >> >> } // End of testing() >> >> and call it from the controller. >> >> Hope that helps. >> >> Regards, >> >> Alfredo >> $this->Model-> >> >> On Mon, Mar 16, 2009 at 2:01 PM, yaman666 wrote: >> >> > Most of my models have an additional property that I was trying to >> > preload by overloadingAppModelconstructor. >> >> > classAppModelextends Model >> > public function __construct() { >> > parent::__construct(); >> > // Load custom property from database and unserialize >> > value >> > } >> > public function beforeSave() { >> > // Serialize and save custom property to database >> > } >> > } >> >> > That broke cakephp when using hasAndBelongsToMany. I filed a bug >> > report (https://trac.cakephp.org/ticket/6194) but was told that I >> > shouldn't be overloadingAppModel. >> >> > Can anyone suggest another simple way to accomplish what I want >> > without having to write each time $model->initialize(); to preload the >> > custom property for all the models? >> >> > Thanks! > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Overloading AppModel constructor
Well, that's exactly what I have. However, how to populate that property from database automatically during model creation without overloading __construct() is what the question is. P.S. I actually figured out that the reason for exceptions was missing parameters in constructor, so now it works, but still interested to see if there's an alternative solution. On Mar 16, 1:50 pm, Alfredo Quiroga-Villamil wrote: > I haven't tried this; but try it and see what happens. > > 1- Create app/models/app_model.php > 2- In there you can have > > classAppModelextends Model { > > protected $sYourPropery = 'property'; > > } // End ofAppModel{} > > All models that extendAppModelshould now have access to > $sYourProperty. In one of your models for example try the following: > > public function testing() { > return $this->sYourProperty; > > } // End of testing() > > and call it from the controller. > > Hope that helps. > > Regards, > > Alfredo > $this->Model-> > > On Mon, Mar 16, 2009 at 2:01 PM, yaman666 wrote: > > > Most of my models have an additional property that I was trying to > > preload by overloadingAppModelconstructor. > > > classAppModelextends Model > > public function __construct() { > > parent::__construct(); > > // Load custom property from database and unserialize > > value > > } > > public function beforeSave() { > > // Serialize and save custom property to database > > } > > } > > > That broke cakephp when using hasAndBelongsToMany. I filed a bug > > report (https://trac.cakephp.org/ticket/6194) but was told that I > > shouldn't be overloadingAppModel. > > > Can anyone suggest another simple way to accomplish what I want > > without having to write each time $model->initialize(); to preload the > > custom property for all the models? > > > Thanks! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Overloading AppModel constructor
I haven't tried this; but try it and see what happens. 1- Create app/models/app_model.php 2- In there you can have class AppModel extends Model { protected $sYourPropery = 'property'; } // End of AppModel{} All models that extend AppModel should now have access to $sYourProperty. In one of your models for example try the following: public function testing() { return $this->sYourProperty; } // End of testing() and call it from the controller. Hope that helps. Regards, Alfredo $this->Model-> On Mon, Mar 16, 2009 at 2:01 PM, yaman666 wrote: > > Most of my models have an additional property that I was trying to > preload by overloading AppModel constructor. > > class AppModel extends Model > public function __construct() { > parent::__construct(); > // Load custom property from database and unserialize > value > } > public function beforeSave() { > // Serialize and save custom property to database > } > } > > That broke cakephp when using hasAndBelongsToMany. I filed a bug > report (https://trac.cakephp.org/ticket/6194) but was told that I > shouldn't be overloading AppModel. > > Can anyone suggest another simple way to accomplish what I want > without having to write each time $model->initialize(); to preload the > custom property for all the models? > > Thanks! > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---