_aFiledNames or how to extend a database-table

Hi folks,

i extended the table oxarticles for one further value, wich is set in the admin, which works fine. The Problem I have is, that i can not access the value in the frontend. The object does not contain the value, and the select-statements also do not contain the new value. Could someone explain, how oxid generates the objects from the database-tables? Where does the array _aFieldNames be initialized?
I am stepping through the code with xdebug for hours now, but did not find the point where things become clear.

Greets, SubNet-One

Hi Subnet,

Yes it is lazy loading functionality. It does not contain any data about new fields, the field is not included in sql selects, it is not loaded and you can not detect it by printing the record UNTIL you actually use the field. So the solution is simple: just try to use this field (eg by printing in template) and it will be loaded automatically every time in current view from now on. It is probably the most complex part of active record functionality, but the good thing is you do not necessary need to know how it works :wink:

Hello folks,

i need some advice again: I have a value in the database, like explained before and I added a getter- and setter-method to the oxarticle-class via module.

public function setMyValue($blMyVal = false)
{
   $this->_blMyValue = $blMyVal;
   return $this->_blMyValue;
}

public function getMyValue()
{
    return $this->_blMyValue;
}

Additionally i added

protected $_blMyValue=false;

where MyValue is the name of the new db-tabel-column in oxarticles. I also extended the save-method of oxarticle by


if ( !isset( $aParams['oxarticles__oxblmyvalue'])) {
    $aParams['oxarticles__oxblmyvalue'] = 0;
} 

The problem is, that the return of [{ if $product->getMyVal() }] is always true, independent of the value (0 or 1) in the database.

I have no Idea, where the problem is and would be grateful for any helpful hint.

Regards, SubNet

Hello again.

I found one mistake I guess: inconsistent naming - sometimes I had blMyValue and MyValue in other cases. Solving this issue ends up in consequent false-returns on call of the getter-function. The read and write in the backend (admin) works flawlessly, but I can not manage to get the current value in the templates.

Could someone maybe give a simle but complete example on how to insert a custom value in the backend and get it from the database in the templates with lazy loading?

One thing comes in my mind right now: what about the values 0 and 1 in the database? Do they represent the boolean answer I want to have properly? As far as I remember oxid uses values of 1 and 0 for active-state for example, too.

Thanks in advance and regards ,

SubNet

SOLVED !!

It is so unbelievably easy :o The solution looks like this:

In the template:


[{ $product->oxarticles__oxblmyvalue->value }]

[B]Important:[/B] use the exact name of the database-field in non-capital letters (oxblmyvalue)!

I only had to alter the database, alter the affected admin-template and use the varue as shown above. It was too simple to see for me.

Well, for anybody with a comparable problem … this is how lazy loading works :smiley:

Hi,

Well, for anybody with a comparable problem …

Thanks for posting the solution, man!

Regards

Isn’t that, how a community-driven board should work :wink:

Hi,

[QUOTE=SubNet-One;15072]Isn’t that, how a community-driven board should work ;)[/QUOTE]

Sure. But you cannot take it for granted. And a simple “thanks for doing that” will put a smile to your face, wouldn’t it? :slight_smile:

Cheers

Yes, mate - you are absolutely right! :slight_smile: