Magic set/get of related records via properties (ORM) [RFC]

800 views
Skip to first unread message

Andres Gutierrez

unread,
Jan 10, 2013, 2:02:27 PM1/10/13
to pha...@googlegroups.com
This RFC is to discuss the implementation of magic methods (read/write properties) in models to read/write related records in addition to the current syntax.

The NFR is here: https://github.com/phalcon/cphalcon/issues/299

The main concern here is the implicit call of magic methods when the model has been defined without its properties:

<?php

class Robots extends Phalcon\Mvc\Model
{
     
}

$robot = new Robots();
$robot->name = "Bender"; //assignment via magic method __set
echo $robot->type //reading via magic method __get

That would not be a problem if the models are defined with their properties. I don't know the current status of the application in this regard.

Please vote whether to include this feature in a next release in the core counting with the little overhead of write/read properties with magic methods if they aren't defined.


Николай Измайлов

unread,
Jan 10, 2013, 3:09:05 PM1/10/13
to pha...@googlegroups.com
function _call($name)
{
   $name = 'get' . $name;
  $this->$name();
}

$this->getName()
$this->setName()
...
+ IDE

It will be good and comfortable. I also suggest, as always not to execute each time _call and all the cache. Appeared method let it be, it is not .

2013/1/10 Andres Gutierrez <andres.g...@phalconphp.com>



--
You received this message because you are subscribed to the Google Groups "Phalcon PHP Framework" group.
To post to this group, send email to pha...@googlegroups.com.
To unsubscribe from this group, send email to phalcon+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Antonio Lopez

unread,
Jan 10, 2013, 8:25:16 PM1/10/13
to pha...@googlegroups.com
+1, this is a must, currently the way to save an entity and its related entities is quite "manual"? compared to other frameworks I think it's very complicated (powerful but complicated), if you think that __set/__get magic methods are a problem we could use a method "add": $product->addCategory($category);

Romanko

unread,
Jan 10, 2013, 8:33:48 PM1/10/13
to pha...@googlegroups.com, andres.g...@phalconphp.com
+1, actually I'd like to see magic getters / setters in models.

First, if properties are stored internally in some kind of a parameter bag (represented with array for instance) it saves performance issue in PHP5.4 

Next, it makes models testing easier if fixtures can be injected into a model via __set_state call

Next, it enabled models prototyping if let's say mode implements something similar to http://au1.php.net/manual/en/class.arrayobject.php

Example:

$resultSet = $database->query('SELECT col1, col2, col3 FROM table WHERE param1 = ?', array('blah'), new ModelPrototype());

foreach($resultSet as $result) {

}

So when you traverse through $resultSet it does 

$result = clone $modelPrototype;
$result->exchangeArray( $statement->fetch(\PDO::FETCH_ASSOC) );

---

In this way you add another way of working with models.

---

As for fetching related records: how are you going to solve N+1 problem?

I really hate when you pull a big resultset in Doctrine and when accessing related records it does another select for each of them. When you normally can SELECT ... JOIN stuff in the most efficient way, ORM normally sucks here.

Nikolaos Dimopoulos

unread,
Jan 11, 2013, 9:22:15 AM1/11/13
to pha...@googlegroups.com
+1

The contents of this message may contain confidential or privileged information and is intended solely for the recipient(s). Use or distribution to and by any other party is not authorized. If you are not the intended recipient, copying, distribution or use of the contents of this information is prohibited.


--
You received this message because you are subscribed to the Google Groups "Phalcon PHP Framework" group.
To post to this group, send email to pha...@googlegroups.com.
To unsubscribe from this group, send email to phalcon+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/phalcon/-/0EKtnlr3lM0J.

Andres Gutierrez

unread,
Jan 11, 2013, 2:19:30 PM1/11/13
to pha...@googlegroups.com
I don't know if an internal bag is fine, most IDEs would not know which fields have the models, so the developer could be leaded to typing errors.
Magic getters/setters also aren't IDE friendly. 

Additionally I think the best practice is to declare the properties in every model class, if the developer does not do that the performance will be slightly reduced and could consume also more memory.

2013/1/10 Romanko <roman....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Phalcon PHP Framework" group.
To post to this group, send email to pha...@googlegroups.com.
To unsubscribe from this group, send email to phalcon+u...@googlegroups.com.

Romanko

unread,
Jan 11, 2013, 4:09:55 PM1/11/13
to pha...@googlegroups.com, andres.g...@phalconphp.com
IDE support can be improved with phpdoc:



I don't know if an internal bag is fine, most IDEs would not know which fields have the models, so the developer could be leaded to typing errors.
Magic getters/setters also aren't IDE friendly. 
 
I agree, but if data is variable it is still OK to go the way which suits you the best :)

Andres Gutierrez

unread,
Jan 14, 2013, 2:29:30 PM1/14/13
to pha...@googlegroups.com
Update: this is how this feature will work, in short, a single 'save' is saving the base model and its related entities.


If anyone wants to try and comment, this is available in the 0.9.0 branch.

2013/1/10 Andres Gutierrez <andres.g...@phalconphp.com>

Nikolaos Dimopoulos

unread,
Jan 14, 2013, 2:34:52 PM1/14/13
to pha...@googlegroups.com
+1

The contents of this message may contain confidential or privileged information and is intended solely for the recipient(s). Use or distribution to and by any other party is not authorized. If you are not the intended recipient, copying, distribution or use of the contents of this information is prohibited.


--
You received this message because you are subscribed to the Google Groups "Phalcon PHP Framework" group.
To post to this group, send email to pha...@googlegroups.com.
To unsubscribe from this group, send email to phalcon+u...@googlegroups.com.

Kenji Minamoto

unread,
Jan 14, 2013, 11:11:03 PM1/14/13
to pha...@googlegroups.com
+1

2013/1/14 Andres Gutierrez <andres.g...@phalconphp.com>

Roman Yanovich

unread,
Jan 31, 2013, 11:05:07 AM1/31/13
to pha...@googlegroups.com, andres.g...@phalconphp.com
+1

четверг, 10 января 2013 г., 21:02:27 UTC+2 пользователь Andres Gutierrez написал:
Reply all
Reply to author
Forward
0 new messages