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?
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');
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.