Results 1 to 4 of 4

Thread: Using Contao 3 models

  1. #1
    User
    Join Date
    04-10-11.
    Posts
    162

    Default Using Contao 3 models

    Hi,

    I've just started developing with Contao 3 and could do with some help using the models class. I have a few questions.

    Do all methods in a model class need to be static?

    Is there a way to use Custom SQL rather than the built in methods like Model::findBy() or Model::findByPK()?

    For example in my model class say I want to get a list of products from the database and each product has tags. This is a many to many relationship, each tag can be related to multiple products and each product can be related to multiple tags. The tables look like this:

    Code:
    products             tags                 product_tags
    id    name           id   name            product_id    tag_id
    In Contao 2 to get the product out of the database with it's tags I'd use something like:

    Code:
    $objProducts = $this->Database->query('SELECT * FROM products');
    
    $arrProducts = $objProducts->fetchAllAssoc();
    
    foreach($arrProducts as $product)
    {
           $objTags = $this->Database->prepare('SELECT tags.name FROM product_tags INNER JOIN tags ON tags.id = product_tags.tag_id WHERE product_tags.product_id = ?')->execute($product['id']);
          
           $product['tags'] = $objTags->fetchAllAssoc();
           $products[] = $product;
    }
    How could I achieve a similar thing using a Contao 3 model class?

    Thank you

  2. #2
    User
    Join Date
    04-10-11.
    Posts
    162

    Default Re: Using Contao 3 models

    Just some extra notes, i've been looking at the 'getRelated' method but i'm not sure how you use it.

    From what I gather you need a model for the related item, but i'm not sure how Contao finds the right table. Also Does this support many to many relationships?

  3. #3
    New user qzminski's Avatar
    Join Date
    01-08-13.
    Location
    Poland
    Posts
    24

    Default Re: Using Contao 3 models

    The model class contains static models to make them easy accessible, but they do not have to be static. If you are writing the model class on your own then it's up to you how are you going to handle things. You can use either static or normal methods. If you would like to provide custom static methods (independent of parent static methods) you can always do this:

    Code:
    public static function findProducts()
    {
        $objProducts = \Database::getInstance()->execute("SELECT * FROM tl_products");
        return $objProducts;
        // or even
        return new \Model\Collection($objProducts, 'tl_products');
    }
    Regarding the getRelated() method, the config is defined in the models DCA. I am not sure if there is a relation "many to many", you need to find it out. Please have a look at the tl_news table and "pid" or "author" fields.

    Code:
    $objNews = \NewsModel::findByPk(123);
    $objAuthor = $objNews->getRelated('author');
    $objNewsArchive = $objNews->getRelated('pid');
    Contao ambassador
    Codefog - Contao web development

  4. #4
    User
    Join Date
    04-10-11.
    Posts
    162

    Default Re: Using Contao 3 models

    Thanks, I'm getting more familiar with it now.

    You can't create a many to many relationship using the default Model class, instead you need to deal with many to many data in a custom method with custom database statements.

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
  •