Skip to content
This repository has been archived by the owner on Sep 4, 2022. It is now read-only.

Create article for object cloning #39

Open
DarkSide666 opened this issue Feb 18, 2016 · 2 comments
Open

Create article for object cloning #39

DarkSide666 opened this issue Feb 18, 2016 · 2 comments
Labels

Comments

@DarkSide666
Copy link
Member

Issue by romaninsh
Sunday Jun 08, 2014 at 13:52 GMT
Originally opened as atk4/atk4#551


Cloning Objects

When you use the PHP clone statement the whole object is duplicated
into an independent variable.

$book_archive = clone $book;

$book_archive->addCondition('is_archive',true);
$book->addCondition('is_archive',false);

$this->add('Grid')->setModel('book');
$this->add('Grid')->setModel('book_archive');

This code will display two grids - one for regular books and another for
archived. Because objects are cloned, adding conditions to one will not
affect the other.

But be careful – there's a gotcha when you clone hooks.

To continue the example above, say you have a hook inside Model_Book
to check a value before saving:

// In Model_Book

function init()
{
    parent::init();

    $this->addField('review');
    $this->hasOne('Author');

    $this->addHook('beforeSave', array($this,'check'));
}

function check($m)
{
    if (strlen($this['review']) < 100) {
        throw $this->exception('Review is too short');
    }
}

After cloning, $this will be referencing the wrong object! Saving
our Model with $book_archive->save() will call $book->check(),
and $this will validate the value of $book instead of
$book_archive.

You can avoid this problem if you use the Model passed in as $m
instead of $this inside a hook. In the above example, $m will
point to $book_archive.

@DarkSide666
Copy link
Member Author

Comment by DarkSide666
Sunday Jun 08, 2014 at 22:11 GMT


Ouch, I never imagined that it happens like this.
This is definitely one aspect to keep in mind while cloning objects!

@romaninsh
Copy link
Member

ATK is not entirely clone-safe. I know that DSQL certainly is clone-safe, the Model - I'm not entirely sure, but Agile data will surely be clone-safe. The rest of the views - I'm not even sure. Probably you can create an article on your blog about usage of Clone.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants