Set custom model attribute

91 views
Skip to first unread message

prakashraman

unread,
May 9, 2013, 3:11:43 PM5/9/13
to redbe...@googlegroups.com
table: user (name, age)

class Model_User extends Redbean_SimpleModel {

   public $url;

   public function __construct(){
      parent::__construct();
      $this->url = "/user/" . $this->name;
   }
}

$user = R::load("user", 1)
echo $user->url;

--- END OF CODE ---

What I would like to know is, if I can create custom class variables for a model and have them initialized at some place. Does redbean currently allow anything like this ?

Thanks.
Message has been deleted

gabor

unread,
May 10, 2013, 2:22:11 PM5/10/13
to redbe...@googlegroups.com
Hi,

You can use dispense() method for this in the model.
You can use this method to set bean properties and model properties.

example:
class Model_Book extends RedBean_SimpleModel {
        public function dispense() {
                $this->bean->url = '/prefilledurl';
        }
}

R::setup('sqlite:test.txt');
R::store(R::dispense('book'));
$a = R::load('book', 1);
echo '>'.$a->url;
echo '>'.R::dispense('book')->url;

http://www.redbeanphp.com/models_and_fuse


cheers,
Gabor

Prakash Raman

unread,
May 10, 2013, 3:16:56 PM5/10/13
to redbe...@googlegroups.com
Great. Thanks a lot. I find this very very useless, as most of models have any custom attributes that need to be set. 


--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
To post to this group, send email to redbe...@googlegroups.com.
Visit this group at http://groups.google.com/group/redbeanorm?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Prakash Raman

Prakash Raman

unread,
May 10, 2013, 3:20:12 PM5/10/13
to redbe...@googlegroups.com
And when I perform a R::find or R::findOne it still, internally beans are loaded right ?

indiekiduk

unread,
Oct 15, 2013, 4:18:58 PM10/15/13
to redbe...@googlegroups.com
Found your post when I was looking for a solution and thought I would post how I eventually solved it:

class Model_User extends Redbean_SimpleModel {

   public $url;

   public function __construct(){
      parent::__construct();
      $this->url = "/user/" . $this->name;
   }
   
//override RedBeans magic getter
   public function __get($prop){
   if($prob == 'url'){
      return $this->url;
   }else{
      return parent::__get($prop);

indiekiduk

unread,
Oct 15, 2013, 4:21:21 PM10/15/13
to redbe...@googlegroups.com
Oh I have just realized the situation is kind of ambiguous. What I was trying to achieve is have a custom variable I can use for business logic in the model that doesn't go the database. You might want your url in the database in which case my solution wasn't for you.

indiekiduk

unread,
Oct 15, 2013, 5:19:27 PM10/15/13
to redbe...@googlegroups.com
woops this is what meta is for isn't it?


On Thursday, May 9, 2013 3:11:43 PM UTC-4, prakashraman wrote:

Zewa One

unread,
Oct 16, 2013, 4:00:23 AM10/16/13
to redbe...@googlegroups.com
In your example if you have overridden __get from OODBBean take care that the method signature is &__get( $property ).
Note the leading & which denotes that the return is done by ref.

Anyway not sure if thats the best way since __GET is doing really a lot of magic which you really skip for your URL prop. If thats what you desire ok, otherwhise ... MEH :)

indiekiduk

unread,
Oct 16, 2013, 10:44:28 AM10/16/13
to redbe...@googlegroups.com
I actually switched from overriding the __get() to using the meta feature and it's much cleaner:

class RedTemplate_Model extends RedBean_SimpleModel{

public function actAs($template){
$templates = $this->bean->getMeta('templates',array());
$templates[] = $template;
$this->bean->setMeta('templates',$templates);
}
public function update(){
foreach($this->bean->getMeta('templates') as $template){
$template->update($this->bean);
}
}
}

On Thursday, May 9, 2013 3:11:43 PM UTC-4, prakashraman wrote:
Reply all
Reply to author
Forward
0 new messages