Welcome Guest, Not a member yet? Register   Sign In
Problem with object property
#1

[eluser]medvind[/eluser]
Hello!
I've got these model classes:

Db_table extends Model
Website extends Db_table
Page extends Website

A function within Db_table fetches values from the database and assigns them to properties within its children, each with the same name as the corresponding database field. One of these properties is site_keywords, a property of Website.

Now, when I try to access this property from Page, using $this->site_keywords, I get an empty value. Using $this->Website->site_keywords works, though. How come?

The full code for these three models is here: http://pastebin.com/m1a49671f

When "manually" assigning a value to $this->site_keywords in Db_table, the value is read just fine. So I suspect this is the faulty code:

Code:
$row_array = $query->result_array();

foreach($row_array[0] as $key => $field_value) {
    $this->$key = $field_value;
}

I don't get what's wrong... Do you?
#2

[eluser]Phil Sturgeon[/eluser]
Why are you looping through the first field in the array?

Try:

Code:
$row_array = $query->row_array();

foreach($row_array as $key => $field_value) {
    $this->$key = $field_value;
}

If that doesn't work, try and print_r() or var_dump() the $row_array and see what is happening.
#3

[eluser]medvind[/eluser]
EDIT: Oh, row_array instead of result_array. Thanks!

Anyhow, the values are acquired OK, but I don't understand why $this->property_name is empty when $this->Website->property_name works (inside of the Page object).
#4

[eluser]jayrulez[/eluser]
try adding $site_keywords to the attributes in the Page class n see what happens
#5

[eluser]medvind[/eluser]
It still returns empty.
#6

[eluser]wiredesignz[/eluser]
If you can access the Website model object separately from the Page model object (even though Page inherits Website) then you must be instantiating and using the two objects separately.

The Page->site_keywords variable instance would definitely be different than the Website->site_keywords variable (variables of different objects), but I can't see why Page->site_keywords would be empty.

Sheesh Tongue

EDIT:
Although... The problem may lay in the Website class $id variable being overidden to NULL by the Page class $id variable and affecting your query.
#7

[eluser]medvind[/eluser]
[quote author="wiredesignz" date="1249404738"]If you can access the Website model object separately from the Page model object (even though Page inherits Website) then you must be instantiating and using the two classes separately.[/quote]

I was suspecting this as well. Both of these models are auto-loaded, but Page needs to have an id passed to it somehow.

If I manually set the id to 1, it works. If I use an object method to do it, it doesn't, even though I haven't created a new instance of the object. I'm confused!
#8

[eluser]wiredesignz[/eluser]
The child class variables/methods always take precedence. So the default value of Website->id is being overidden by Page->id.
#9

[eluser]medvind[/eluser]
[quote author="wiredesignz" date="1249405453"]The child class variables/methods always take precedence. So the value of Website->$id is being overidden.[/quote]

That's OK by me! My intention was that the Website constructor would first load the website settings into the object properties. After that, the ID will be overwritten, as will table_name, so that the individual Page's settings will be loaded too, into their own object properties.
#10

[eluser]wiredesignz[/eluser]
Try deleting the Page->id variable entirely but leaving the Website->id to be used by both objects.




Theme © iAndrew 2016 - Forum software by © MyBB