SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 57
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to setup Models?

    I'm trying to figure out how to setup the model part of MVC.

    I have studied different ways that other frameworks are using of setting up models.

    Some use:

    ActiveRecord
    Propel
    Datamapper

    My Questions:

    1. Does anyone have advice which route I should go down?
    2. Can you please post some explamples of how you use models?

  2. #2
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is doing it the this way acceptable?


    PHP Code:
    <?php
    class ModelProduct extends Model {    
        function 
    getProduct($product_id) {
            return 
    $this->database->query("select * from product where product_id = '" . (int)$product_id "'");
        }
         
        function 
    listProduct($language_id$sort 'pd.name'$limit 20) {
            return 
    $this->database->query("select * from product p left join product_description pd on (p.product_id = pd.product_id) where pd.language_id = '" . (int)$language_id "'");
        }
        
        function 
    countProduct() {
            
    $result $database->database("select * from product");
            
            return 
    $result->countRows();
        }
        
        function 
    searchProduct($where$sort 'pd.name'$limit 20) {
            
    $result $this->database->query("select * from product where `" $this->database->escape($key) . "` = '" $this->database->escape($value) . "'");
            
            return 
    $result->getRows();
        }
                
        function 
    insertProduct($data) {
            
    $this->database->query("insert into product set quantity = '?', date_available = '?', model = '?', manufacturer_id = '?', image_id = '?', shipping = '?', price = '?', sort_order = '?', weight = '?', weight_class_id = '?', status = '?', tax_class_id = '?', date_added = now()");
            
            
    $this->cache->delete('product');
            
            return 
    $this->database->getLastId();
        }
        
        function 
    updateProduct($product_id$data) {
            
    $this->database->query("update product set quantity = ':quantity', date_available = ':date_available', model = ':model', manufacturer_id = ':manufacturer_id', image_id = ':image_id', shipping = ':shipping', price = ':price', sort_order = ':sort_order', weight = ':weight', weight_class_id = ':weight_class_id', status = ':status', tax_class_id = ':tax_class_id', date_modified = now() where product_id = '" . (int)$product_id "'");
            
            
    $this->cache->delete('product');
            
            return 
    $this->database->countAffected();
        }
        
        function 
    deleteProduct($product_id) {
            
    $this->database->query("delete from product where product_id = '" . (int)$product_id "'");
            
            
    $this->deleteDescription($product_id);
            
    $this->deleteOption($product_id);
            
    $this->deleteDiscount($product_id);
            
    $this->deleteImage($product_id);
            
    $this->deleteDownload($product_id);
            
    $this->deleteCategory($product_id);
            
            
    $this->cache->delete('product');
            
            return 
    $this->database->countAffected();
        }

    etc...
    ?>

  3. #3
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    Is doing it the this way acceptable?
    That's whats called a table data gateway. It's different than an active record or a data mapper, but it is a perfectly acceptable way of interacting with a database. IF you're looking for 3rd party code to use, I would recommend doctrine over propel. The examples and documentation should help you get a handle on how it's used.

  4. #4
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks doctrine looks quite good.

    I have another 2 questions?

    1. How do you use your models?

    $model->load('product');

    $model->product->update($product_id, $data);

    or

    $product_model =& $model->get('product');

    $product_model->update($product_id, $data);


    2. How do you handle loading your models inside other models?

    I just need some examples.
    Last edited by blueyon; Oct 30, 2007 at 10:44.

  5. #5
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you normally create one model for one table?

    Or can you have:

    PHP Code:
    class ModelProduct extends Model {
        function 
    getProduct($product_id) {
        }
         
        function 
    listProduct($language_id$sort 'pd.name'$limit 20) {
        }
        
        function 
    countProduct() {
        }
        
        function 
    searchProduct($where$sort 'pd.name'$limit 20) {
        }
                
        function 
    insertProduct($data) {

        }
        
        function 
    updateProduct($product_id$data) {

        }
        
        function 
    deleteProduct($product_id) {

        }
        
        function 
    getDescription($product_id$language_id) {
        }
        
        function 
    insertDescription($product_id$language_id$data) {
        }
        
        function 
    deleteDescription($product_id) {
        }
        
        function 
    getOption($product_id) {
        }
        
        function 
    insertOption($product_id$language_id$option_id
        }

        function 
    deleteOption($product_id) {
        }

        function 
    getDiscount($product_id) {
        }
        
        function 
    insertDiscount($product_id$data) {
        }    

        function 
    deleteDiscount($product_id) {
        }    
    }
    ?> 

  6. #6
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    Do you normally create one model for one table?
    Usually there's one object per table, and when you're using active record, you have an object that is used to represent a single row:

    PHP Code:

    $finder 
    = new ProductFinder();
    $product_model $finder->find($product_id);
    $product_model->assign($data);
    $product_model->save(); 

  7. #7
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the idea is to use one object per table how are you soposed to do joins?

    This is the part that confuses me the most.

  8. #8
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how are you soposed to do joins?

    Write your model objects for components (or other logical parts), not for individual tables.

  9. #9
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mastodont View Post
    how are you soposed to do joins?

    Write your model objects for components (or other logical parts), not for individual tables.
    So I was right in the first place by doing:

    PHP Code:
    <?php
     
    class ModelProduct extends Model {     function getProduct($product_id) {     }          function listProduct($language_id$sort 'pd.name'$limit 20) {     }          function countProduct() {     }          function searchProduct($where$sort 'pd.name'$limit 20) {     }                  function insertProduct($data) {      }          function updateProduct($product_id$data) {      }          function deleteProduct($product_id) {      }          function getDescription($product_id$language_id) {     }          function insertDescription($product_id$language_id$data) {     }          function deleteDescription($product_id) {     }          function getOption($product_id) {     }          function insertOption($product_id$language_id$option_id,     }      function deleteOption($product_id) {     }      function getDiscount($product_id) {     }          function insertDiscount($product_id$data) {     }          function deleteDiscount($product_id) {     }     } ?>

  10. #10
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    So I was right in the first place by doing:
    Well, that's one way of doing it, it's not necessarily the "right" way. You can have data returned by a join populate multiple objects, one for each table.

  11. #11
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees View Post
    Well, that's one way of doing it, it's not necessarily the "right" way. You can have data returned by a join populate multiple objects, one for each table.
    How would you do it though?

  12. #12
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is not your model, the problem is the relational database you are using. Relational databases use a series of tables (not objects) to organize data. So, why should you bother to do something that the database is not doing for you?

    In theory, the mapping of the objects should be done in the database, and it's a shame we have to perform an object-relational mapping after we receive the data, when this should clearly be done in the database.

    Anyway, relational databases are a big mistake, and they force you to use your own ORM tool.

    There's a clear mismatch between the architectural approach of the object oriented application and an approach in which the data is stored in a relational database management system. Unfortunately, we have to live with it.

  13. #13
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    blueyon:

    Well, if your class ModelProduct extends Model, what would be in class Model? Inheritance is one good reason for extending classes, and your ModelProduct contains only specific methods (getProduct, listProduct etc.).

    Wouldn't it be better to define general methods get, list, count etc. in class Model and overwrite them in derived classes where necessary? Class Model must - of course - have some property for name of main table ...

    So instead of

    PHP Code:
        function getProduct($product_id) { 
            return 
    $this->database->query("select * from product where product_id = '" . (int)$product_id "'"); 
        } 
    you could have in Model

    PHP Code:
        function get($id) { 
            return 
    $this->database->query("select * from " $this->table " where id = '" . (int)$id "'"); 
        } 

  14. #14
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mastodont View Post
    blueyon:

    Well, if your class ModelProduct extends Model, what would be in class Model? Inheritance is one good reason for extending classes, and your ModelProduct contains only specific methods (getProduct, listProduct etc.).

    Wouldn't it be better to define general methods get, list, count etc. in class Model and overwrite them in derived classes where necessary? Class Model must - of course - have some property for name of main table ...

    So instead of

    PHP Code:
        function getProduct($product_id) { 
            return 
    $this->database->query("select * from product where product_id = '" . (int)$product_id "'"); 
        } 
    you could have in Model

    PHP Code:
        function get($id) { 
            return 
    $this->database->query("select * from " $this->table " where id = '" . (int)$id "'"); 
        } 
    I can understand what your saying and it makes sense, But what do you do about joins from one table to another that are not one component?

  15. #15
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One logical component could employ more tables. So joins from one table to another that are not one component are IMHO matter of wrong design. You could always write methods in extended classes (or base class) with SELECT FROM more tables.

  16. #16
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my opinion it doesn't reallly matter how your are using your models. A model is just a layer to access data.

    A model could interact with a database but also xml files. It's just a matter of design how you are going to use your models in whatever application.

    Par example in my framework I use plain queries to access the database. The queries can interact with one table or many tables.

  17. #17
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is not impossible to have models represented by joins, even in moderately complex systems. But if such models are needed on a regular basis they're much better represented in the DB itself using a view, which can be easily accessed as a standard table.

  18. #18
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it helps to think of models not as a way of accessing data from a database, but rather think of a database as a way to persist a model. You start by designing the objects, then you use the database to save them, and you'll find that it's quite rare that a single object would need to be stored in several tables. There are times when you'll need to do complex SQL for reports, etc, but that is a different issue than persisting objects.

  19. #19
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mastodont View Post
    One logical component could employ more tables. So joins from one table to another that are not one component are IMHO matter of wrong design. You could always write methods in extended classes (or base class) with SELECT FROM more tables.
    Its not bad design!

    What about if you had to get a product information and you wanted to left join to the image table to pull out the image filename? or manufacturer? etc..

    I prefer to keep my images in one table to because its easier to manage.

    Are you soposed to do a query per table to pull the information out?

  20. #20
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    What about if you had to get a product information and you wanted to left join to the image table to pull out the image filename? or manufacturer? etc..

    I prefer to keep my images in one table to because its easier to manage.
    As I said above: views, views, views.

  21. #21
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac View Post
    As I said above: views, views, views.
    How the hell is something like that supposed to work!

    I demand examples!

  22. #22
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    How the hell is something like that supposed to work!

    I demand examples!
    A view is basically a virtual table defined by a (usually complex) select query. It's not the result set, it's more like the result set as a database table. (Which you can query again).
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  23. #23
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    What about if you had to get a product information and you wanted to left join to the image table to pull out the image filename? or manufacturer? etc..

    I prefer to keep my images in one table to because its easier to manage.
    You could consider that products and images are seperate objects. i.e. $product->image.

    Quote Originally Posted by blueyon View Post
    Are you soposed to do a query per table to pull the information out?
    Depends on how the ORM is implemented. With doctrine:

    PHP Code:

    $user 
    Doctrine_Query::create()
            ->
    from('User u')
            ->
    leftJoin('u.Email e')
            ->
    leftJoin('u.Phonenumber p')
            ->
    leftJoin('u.Group g')
            ->
    execute();

    print 
    $user->Email['address'];

    print 
    $user->Phonenumber[0]->phonenumber;

    print 
    $user->Group[0]->name

    Incidentally, demanding examples will probably make people less likely to want to help you.

  24. #24
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    How the hell is something like that supposed to work!

    I demand examples!
    I'm sorry, but I don't time right now for anything more than this.

  25. #25
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees View Post

    Incidentally, demanding examples will probably make people less likely to want to help you.
    I was trying to be funny.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •