Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Relations between models issue

edited December 2014 in Framework

Considering these three models Artist, Artwork and Photo (tables artists, artworks and photos), I can have several artworks for each artist and several photos for each artwork.

These are the names of the keys: artists: id (PK) artworks: id (PK), artist (FK) photos: id (PK), artwork (FK)

I described these relationships in this way:

Artist

namespace artists;

class Artist extends \Model {
    public $table = '#prefix#artists';
    public $key = 'id';
    public $fields = array (
        'artworks' => array ('has_many' => 'Artwork')
    );
}

Artwork

namespace artworks;

class Artwork extends \Model {
    public $table = '#prefix#artworks';
    public $key = 'id';
    public $fields = array (
        'artist' => array ('belongs_to' => 'Artist'),
        'photos' => array ('has_many' => 'Photo')
    );
}

Photos

namespace photos;

class Photo extends \Model {
    public $table = '#prefix#photos';
    public $key = 'id';
    public $fields = array (
        'artwork' => array ('belongs_to' => 'Artwork')
    );
}

Is that correct?

Following the examples here https://www.elefantcms.com/docs/2.0/developers/modelling-your-data I created some handlers like this

$artwork = new artworks\Artwork (2);

var_dump($artwork);
echo "<hr><br>";

$artist = $artwork->artist ();
var_dump($artist);
echo "<hr><br>";

$photos = $artwork->photos ();

foreach ($photos as $photo) {
    //printf ('%s<br />', $item->name);
    var_dump($photo);
    echo "<hr><br>";
    }

but I have a problem; while $artwork is dumped as expected the call of the dynamic methods $artwork->photos() gives an error like:

Fatal error: Class 'Artist' not found in ...\lib\Model.php on line 274

Fatal error: Class 'Photo' not found in ...\lib\Model.php on line 302

What am I doing wrong?

Comments

  • edited December 2014

    I notice that the query built to retrieve $photos is:

    select * from `t_photos` where `t_artworks` = ?
    

    while it should be

    select * from `t_photos` where `artworks` = ?
    

    lib\Model.php (line 295)

    but also correcting the query I get the error:

    Fatal error: Class 'Photo' not found in ...\lib\Model.php on line 302

  • Firstly, your namespaces are not matching up. Each class is being called from within a different namespace, thus are unable to find the desired class because it's not declared with the same namespace or the namespace isn't specified.

  • Thanks, BronyBorn. I followed an example of the documentation and I was expecting that were the class Artwork to load other needed classes. What would I change?

  • Either change the namespace values to match, or add the namespace value to the targeted class values like:

    public $fields = array (
        'artist' => array ('belongs_to' => 'artists\Artist'),
        'photos' => array ('has_many' => 'photos\Photo')
    );
    

    I'm pretty sure that either would solve your problem, though I suggest simply making sure each of those classes exist in the same namespace if all those classes are within the same app. Elefant's general convention for namespaces is anything outside of the core lib uses the name of the app that the class is stored in, and cross reference namespaces across apps as needed.

  • Ahhhh! Do you know when you're set on one thing and not see its neighbor? This is it! Thank you so much!

Sign In or Register to comment.