DooPHP IRC channel


Base DooModel Class for all models

Discussion about new desired features.

Base DooModel Class for all models

Postby kostik » Tue Aug 25, 2009 4:53 pm

I am proposing to add a base class DooModel from which all other models will be extended. The main purpose of this class is to provide a bunch of useful methods which should be inherited by all models. for example i propose the following interface

Code: Select all
abstract class DooModel{

    public function __construct($assocArray = NULL) {
      if ($assocArray != NULL) {
        $this->update($assocArray);
      }
    }

    public function update($assocArray = NULL) {
      $myAttributes = get_object_vars($this);
      foreach ($myAttributes as $attribute => $value) {
        if( isset($assocArray[$attribute]) ) {
          $this->$attribute = $assocArray[$attribute];
        }
      }
    }

    public function save()  {
      return $this->db()->insert($this);
    }

    public function delete()  {
      return $this->db()->delete($this);
    }

    public function getById()  {
      return Doo::db()->find($this, array('limit'=>1));
    }

    public function getAll()  {
      return Doo::db()->find(get_class($this));
    }
   
   public function getByField($fieldName, $fieldValue) {
      // to do
   }
}


So as you can see this will simplify retrieving, deleting and saving objects. Also you can create object from a associative array which could be your form data.
so instead of saying $article = new Article; $article->name = $_POST['name']; $article->text = $_POST['text']; you can say $article = new Article($_POST);
Also right here we can implement caching (this is another issue).

DB Caching
Concept is simple for now,
When u getById, cache result, when u delete/save, invalidate the entry

Waiting for feedback.
kostik
 
Posts: 17
Joined: Fri Aug 14, 2009 4:46 pm

Re: Base DooModel Class for all models

Postby kostik » Wed Aug 26, 2009 3:17 pm

no one has any thoughts at all?
kostik
 
Posts: 17
Joined: Fri Aug 14, 2009 4:46 pm

Re: Base DooModel Class for all models

Postby leng » Wed Aug 26, 2009 9:11 pm

The extra query methods will be really useful. On the cache, we will need to think of a nice way to implement it.
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: Base DooModel Class for all models

Postby roman » Wed Aug 26, 2009 11:57 pm

Where would form input data validation fit?
roman
 
Posts: 442
Joined: Sat Aug 01, 2009 8:31 pm

Re: Base DooModel Class for all models

Postby kostik » Thu Aug 27, 2009 12:19 am

Roman, that is up to you. The concept merely states that u could create a model from an associative array.
But to address validation you could for example create something like this.

Code: Select all
private $_rules=array('name'=>'[a-z]/i','phone'=>'[0-9_-.\s]'); //which is an associative array of expected field names and regular expression to validate;

public function validate() //this would validate and return $errors array


so u can create an object from $_POST and then validate() object and return any $errors
I think we could implement DooForm class which would handle all of the form's stuff.
kostik
 
Posts: 17
Joined: Fri Aug 14, 2009 4:46 pm

Re: Base DooModel Class for all models

Postby leng » Thu Aug 27, 2009 12:32 am

Hey guys did you miss out the update_attributes and insert_attributes in DooSqlMagic? It is meant for $_POST or $_GET stuffs
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: Base DooModel Class for all models

Postby kostik » Thu Aug 27, 2009 12:47 am

leng, do u ever sleep?
and yes, I haven't got to those yet.
kostik
 
Posts: 17
Joined: Fri Aug 14, 2009 4:46 pm

Re: Base DooModel Class for all models

Postby leng » Thu Aug 27, 2009 1:24 am

I am on an abnormal routine... sometimes* :?
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: Base DooModel Class for all models

Postby roman » Thu Aug 27, 2009 5:03 am

Kostik, that was a good tip. Thanks. And I like the idea of having a class or something that would handle most, if not all, things related to handling form input.

leng wrote:Hey guys did you miss out the update_attributes and insert_attributes in DooSqlMagic? It is meant for $_POST or $_GET stuffs

Is that intended to do something along the lines of what kostik was talking about?

leng wrote:I am on an abnormal routine... sometimes* :?

Leng, as a short off-topic comment for you (it applies to me too as my day schedule is abnormal as well), take a look at http://www.pslgroup.com/dg/13D2F2.htm. That, combined with other articles in http://www.google.com/search?q=study+healthy+subjects+sleep+5+hours+hormones, should inspire one to sleep enough night hours :) I am dropping everything right now and going to bed while it's still just little after midnight :)
roman
 
Posts: 442
Joined: Sat Aug 01, 2009 8:31 pm

Re: Base DooModel Class for all models

Postby Ming » Thu Aug 27, 2009 8:50 am

hi

i think its not so good , because you can't inheriting your classes later
better : OrmEngine::loadValue( UserModel, $array );

like pojo pattern in java

Ming
Ming
 
Posts: 3
Joined: Thu Aug 27, 2009 8:45 am

Next

Return to Features Request

Who is online

Users browsing this forum: No registered users and 0 guests

cron