Currently ‘with’ is only possible when retrieving records.
When creating a AR instance, for use of adding a new row or for a form, all relations need to be manually created.
Instead of doing this:
$model = new Post(); $model->author = new Author();
Why not something like this:
$model = new Post(array('with'=>array('author')));
If it’s many-to-many or one-to-many then it could either create an array with the related AR instance for only the first item, or treat it like a one-to-one.
Actually this might work well:
$model = new Post(array('with'=>array('category'))); //produces $model = new Post(); $model->author = new Author(); //while this $model = new Post(array('with'=>array('category'))); //produces $model = new Post(); $model->author = array(new Author(), new Author());
It would also be good if CHtml::resolveName would recognize relations for the attribute. Currently, after manually creating the relation, this is usually done:
CHtml::activeName($model->author, "name"); // returns "Author[name]"
But it would be good nice to keep the relation together like so:
CHtml::activeName($model, "author.name"); // returns "Post[author][name]"
Some exception would need to be made for tabular input so it can be figured out if "Post[author][$i][name]" or "Post[$i][author][name]" is desired.
It would also need to work for chains of relations just like it does for criteria:
CHtml::activeName($model, "author.address.street"); // returns "Post[author][address][street]"
[size="1"]Of course it would also be nice if:[/size] [size="1"]
$post->attributes = $_POST['Post'];
[/size] [size=“1”]would assign $_POST[‘Post’][‘Author’] to $post->author and if validate would work as well, but that’s beyond this suggestion.[/size]
Don’t know if this is a horrible idea or not, I hope not. Seems every form I’ve ever made is normalized in some way, so I come across this all the time. I’m also aware of the cadvancedarbehavior and eadvancedarbehavior.