Welcome Guest, Not a member yet? Register   Sign In
Related objects
#1

[eluser]Unknown[/eluser]
Hi all,

I am new here and pulling my hair out right now!

I searched through the forums, done all the obvious things but just can not see what is wrong.

Any help would be really appreciated!

I have a few tables, with junctions tables too.

schools,
websites,
numbers,
websites_schools,
numbers_schools.


A school will have many websites, and many numbers.

Code:
class School extends DataMapper
{
    public $has_many = array('number', 'website');
}

A website can have many schools.

Code:
class Website extends DataMapper
{
    public $has_many = array('school');
}

A number can have one school.

Code:
class Number extends DataMapper
{
    public $has_one = array('school');
}

So I get a school by a given ID like so ...

Code:
$schools = new School();
return $schools->get_by_id($id);

I then can get the numbers assosiciated with a school like so ...

Code:
<!-- Telephone numbers -->
    <? if ($school->number->count() >= 1): ?>
        <ol>
            &lt;? foreach ($school->number->get() as $number): ?&gt;
                <li><strong>&lt;?= $number->type ?&gt;:</strong> &lt;?= $number->digits ?&gt;</li>
            &lt;? endforeach ?&gt;
        </ol>
    &lt;? endif ?&gt;

Works great, but not for the websites.

Code:
&lt;!-- Web sites --&gt;
    &lt;? if ($school->website->count() >= 1): ?&gt;
        <ol>
            &lt;? foreach ($school->website->get() as $website): ?&gt;
            <li><strong>&lt;?= $website->label ?&gt;:</strong> &lt;?= $website->url ?&gt;</li>
                &lt;? endforeach ?&gt;
        </ol>
    &lt;? endif ?&gt;

Pretty much identical but I get the error ... Fatal error: Call to a member function count() on a non-object in ... or Fatal error: Call to a member function get() on a non-object in ...

Checked everything over and over and just can't see it, as one works but one does not.

Database is autoloaded, as is library DataMapper. Tables names are plural, models singular. Capital letters on model names first character, lower case file names.

Really Confused! Any ideas?
#2

[eluser]jonez[/eluser]
Use var_dump to debug, if you are getting that error something is wrong with your objects. I don't use ORMs but I imagine it's a bad relationship that isn't producing the mapping you expect.

Code:
&lt;!-- Web sites --&gt;

&lt;?php
//this should be an object, compare with a working one
die(var_dump($school->website));
?&gt;

    &lt;? if ($school->website->count() >= 1): ?&gt;

&lt;?php
//this should be an array
die(var_dump($school->website->get()));
?&gt;

        <ol>
            &lt;? foreach ($school->website->get() as $website): ?&gt;
            <li><strong>&lt;?= $website->label ?&gt;:</strong> &lt;?= $website->url ?&gt;</li>
                &lt;? endforeach ?&gt;
        </ol>
    &lt;? endif ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB