addParentField()

addParentField($name)

Create a reference to a parent field
@param String $name
@return PhpBURN_Map

Params


$name
The field that should link with parent Model Class

Explanation


This is a pioneer resource that PhpBURN offer since the very first beta version. Imagine if you could create a Model than you need to extend that Model and keep working with existing data independent of their Children. Now imagine more, imagine that you need to extend the same Model for some reason without conflict with all existing data and inheritance. Now imagine even more, imagine that you need to extend the Children that you already have that extends a Model and all this should work transparently, easly and without conflicts or troubles.

Well, stop imagine and DO IT! With PhpBURN you can.

All you need is to have Your Models mapping, lets see an Example:

Persons Model

class Persons extends PhpBURN_Core {
    public $_tablename = 'persons';
    public $_package = 'phpburn';

    public function _mapping() {
        $this->getMap()->addField('id_people','id_people','int',10,array('auto_increment' => true, 'primary' => true, 'not_null' => true))
        ->addField('name','name','varchar',255,array('not_null'))
        ->addField('age','age','int',3,array());
    }
}

User Model

class Users extends Persons {
    public $_tablename = 'users';
    public $_package = 'phpburn';

    public function _mapping() {
        $this->getMap()->addField('id_user','id_user','int',10,array('auto_increment' => true, 'primary' => true, 'not_null' => true))
        ->addField('username','username','varchar',255,array('not_null'))
        ->addField('password','password','varchar',255,array())
        ->addField('id_people','id_people','int',10,array('not_null' => true));

        $this->getMap()->addParentField('id_people');
    }
}

Look how we did to LINK one Model to Another by creating the same KEY field in both tables and models and then saying to the model: $this->getMap()->addParentField(‘id_people’);

In resume, we can have MANY Persons but NOT every Persons CAN have a USERS but is MANDATORY every USERS is a PERSONS.

Now let’s see how can we use it:

$user = new Users();

$user->get(1);

print_r($user->toArray());

This will produce: “Array(‘id_user’ => ‘1’, ‘username’ => ‘klederson’, ‘password’ => ‘phpburn’, ‘id_person’ => ‘341’, ‘name’ => ‘Klederson Bueno’, ‘age’ => ’24’)”

Now let’s understand what happens here…

Note that you have called as a NORMAL model what is a inheritance that’s the magic, you can use multiples models as only one… it will work like ONE_TO_ONE relationships but more transparent you can choose between inheritance or relationship by defining your business model.

You can see the id_user and id_person are different right? That is because Persons are 100% independent from users and are Other object in our system used for other reasons it means you can use Persons directly whithout have to worry about Users or any other chindren that Persons can have.

Now let’s take a look in PhpBURN saving it:

$user = new Users();

$user->get(1);

$user->name = "Klederson Bueno Bezerra da Silva";
$user->password = "123456";

$user->save();

That will produce two UPDATES, one into the users table and another into the persons table, if you had not changed something from persons table it will only perform a UPDATE into users table. PhpBURN handles automaticaly changed data. It will work for a insert too take a look:

$user = new Users();

$user->name = "Klederson Bueno Bezerra da Silva";
$user->password = "123456";
$user->username = "klederson2";
$user->age = "24";

$user->save();

That’s it you dont need to refeer to the key field because it will be assigned automaticaly. Pretty cool uh?
You can do anything you want with this so…
BE CREATIVE.