I had many problems with has_many-through relationships but finally I found nice example here which solved most of my problems. However, according to code presented below I have couple questions.
firstly, code:
$artists = ORM::factory('artist')->find_all();
foreach ( $artists as $artist )
{
foreach ( $artist->media->find_all() as $m )
{
echo $m->name;
}
}
1) This example is probably controller. What if I want to store media in $artists to send one variable to view? Is it possible to store media as media property in artist object? (I mean for example $artists[0]->media[0]->name)
2) Is it possible to completely load $artists without this loop?
1) If I understood correctly, you need to get some element from medias
$artists = ORM::factory('artist')->find_all()->as_array();
$media = $artists[0]->media->find_all()->as_array(); // media of first artist
$name = $media[0]->name;
2) See above $artists is an array of ORM objects
Following my comment, this is what I'd do.
class Model_Artist extends ORM {
///
/// Whatever you have now
///
private $_media_cache = NULL;
public function media($key = NULL)
{
// Check cache
if($this->_media_cache == NULL)
{
$this->_media_cache = $this->media->find_all();
}
if($key !== NULL)
{
// Use Arr::get in case index does not exist
// Return empty media object when it does not exist so you can
// 'harmlessly' ask for its properties
return Arr::get($this->_media_cache, $key, ORM::factory('Media'));
}
return $this->_media_cache;
}
}
Callable as
$artists[0]->media(0)->name
Related
I'm Trying to create single array that contains all the ID of parent and child from the database.
But all I was getting is single data.
My ideal output is:
array('160', '161', '162', '163', '164');
what am I getting is only
array('160');
Here is what I've done so far.
public function arrayId(array $elements) {
$where_in = array();
foreach($elements as $element){
if($element->isArray) {
$elems = $this->my_model->select_where('tbl_policies', array('parent_id' => $element->id));
$this->arrayId($elems);
}
$where_in[] = $element->id;
}
return $where_in;
}
$id = 160; //for instance
$elements = $this->my_model->select_where('tbl_policies', array('id' => $id));
$where_in = $this->arrayId($elements);
die(print_r($where_in));
and the data I'm fetching here:
tbl_policies
It's kinda difficult for me to construct questions. So please if something is not clear, do comment below, I'll try my best to make it more understandable. Thanks in advance.
I understand, that you want to delete a parent with all its children and grandchildren. But you do it not directly and sequentially rather want to collect all ids of the records to be deleted. You should go following steps:
Parent-Id (example 160) is already known. Add this to your list.
Write a recursive function such as getChildrenIds(parentId).
Within this function you should iterate over children. And if a child has the flag "isArray" (according to your application logic) then you should call getChildrenIds(currentChildId)
I have written following function. It should work.
public function getChildrenIds( int $parentId, array &$idList) {
$idList[] = $parentId;
$records = $this->my_model->select_where('tbl_policies', array('parent_id' => $parentId));
foreach($records as $r){
if($r->isArray)
$this->getChildrenIds($r->id, $idList);
else
$idList[] = $r->id;
}
return;
}
public function CollectIds(){
$id = 160; //for instance
$where_in = array();
$this->getChildrenIds($id, $where_in);
}
Please notice, that $where_in passed by reference to the recursive function getChildrenIds() and filled there.
I don't want to modify the object when ever I call it, and I am looking that upon call, the modifications are done automatically.
In a controller, I have the following:
$candidate = Candidate::where('slug', '=', $slug)->first();
And then I do:
if (!is_null($candidate->social_profiles) && !empty($candidate->social_profiles)) {
$candidate->social_profiles = unserialize($candidate->social_profiles);
foreach ($candidate->social_profiles as $key => $value) {
$candidate->{$key} = $value;
}
$candidate->social_profiles = null;
}
Now, I am looking for a way, to do the last part, inside the very own Candidate model, so that whenever a candidate is retrieved, it does the code above, that way I wont have to duplicate the last part above, wherever I get a candidate.
The problem is, that I don't know where to do so. $this in __construct contains only data defined in the class, which is basically an empty model. What else?
This is probably personal taste, but I would create a CandidateRespository class that has a method findBySlug (or whatever you'd like to call it):
public function findBySlug($slug)
{
$candidate = Candidate::where('slug', '=', $slug)->first();
if (!is_null($candidate->social_profiles) && !empty($candidate->social_profiles)) {
$candidate->social_profiles = unserialize($candidate->social_profiles);
foreach ($candidate->social_profiles as $key => $value) {
$candidate->{$key} = $value;
}
$candidate->social_profiles = null;
}
return $candidate;
}
Pass the repository into your controller using dependancy injection and then call it using:
$candidate = $this->candidate->findBySlug($slug);
Here's some more reading: http://laravel.com/docs/ioc & http://culttt.com/2013/07/08/creating-flexible-controllers-in-laravel-4-using-repositories/
Hope that helps.
So I've got a plugin which produces information for me based on a user's id.
This happens in the plugin's module 'pitch':
public function executeIndex(sfWebRequest $request)
{
$unique_id = $request->getParameter('unique_id');
$this->user = UserTable::getInstance()->getByToken($unique_id);
$this->forward404Unless($this->user);
$this->iplocation=new IPLocation();
$qualified_offers = new QualifiedOffers();
$this->creatives = $qualified_offers->applicableTo($this->user);
$this->match_type = UserDemoTable::getInstance()->getValue($this->user->id, 'match');
// Put the applicable creatives into the session for later use
$userCreatives = $this->creatives;
$this->getUser()->setAttribute('userCreatives', $userCreatives);
}
And then I try to call that attribute on the subsequent template (In a different module called 'home' with a different action):
public function executePage(sfWebRequest $request)
{
$template = $this->findTemplate($request->getParameter('view'), $this->getUser()->getCulture());
$this->forward404Unless($template);
$this->setTemplate($template);
// Grab the creatives applicable to the user
$userCreatives = $this->getUser()->getAttribute( 'userCreatives' );
}
Unfortunately it doesn't work at all.
If I try this from the action where $creatives is initially generated:
$this->getUser()->setAttribute('userCreatives', $userCreatives);
$foo = $this->getUser()->getAttribute('userCreatives');
// Yee haw
print_r($foo);
I am met with great success. I'm essentially doing this, only from two different controllers. Shouldn't that be irrelevant, given that I've added 'userCreatives' to the user's session?
It sounds like you're trying to store objects as user attributes (i.e., in the session).
From Jobeet Day 13:
You can store objects in the user session, but it is strongly discouraged. This is because the session object is serialized between requests. When the session is deserialized, the class of the stored objects must already be loaded, and that's not always the case. In addition, there can be "stalled" objects if you store Propel or Doctrine objects.
Try storing either array or stdClass representations of your objects and then loading them back into "full" objects once you retrieve them.
Here's an example that I used on another project:
class myUser extends sfGuardSecurityUser
{
...
public function setAttribute( $name, $var )
{
if( $var instanceof Doctrine_Record )
{
$var = array(
'__class' => get_class($var),
'__fields' => $var->toArray(true)
);
}
return parent::setAttribute($name, $var);
}
public function getAttribute( $name, $default )
{
$val = parent::getAttribute($name, $default);
if( is_array($val) and isset($val['__class'], $val['__fields']) )
{
$class = $val['__class'];
$fields = $val['__fields'];
$val = new $class();
$val->fromArray($fields, true)
}
return $val;
}
...
}
I have a static method 'findAll' on a model which basically gets all rows with certain criteria. This method works fine and I can call it using:
$m::findAll();
Where $m is the model name as a variable. I can output this and it returns correct results. However, when assigning this to a variable in the Zend_View object, as:
$this->view->viewvariable = $m::findAll();
I get the error:
Zend_Db_Table_Exception: Too many
columns for the primary key
Any ideas why?
Find all function:
final public static function findAll($where = false, array $options = array()) {
$object = new static();
if (!empty($options)) $options = array_merge($object->options, $options);
else $options = $object->options;
$run = $object->buildDefaultSelect($where, $options);
$rows = $run->fetchAll();
if ($options['asObject'] == true) {
$result = array();
foreach ($rows as $r) {
$class = new static();
$class->setInfo($r);
$result[] = $class;
}
return $result;
} else {
if (count($rows) > 0) return $rows;
else return array();
}
}
Note: This function works fine everywhere apart from when assigning to a view variable. If I run the below (not assigning it to a view variable), it shows the correct array data.
var_dump($m::findAll($module['where'], $module['options']));
exit;
In my view (I have replaced the actual name with viewvariable for the sake of this post):
<?php foreach($this->viewvariable as $item) { ?>
//Do some echoing of data in $item
//Close foreach
I doubt the issue is with Zend_View. It's hard to tell without seeing your code, but my guess is that findAll() is using the Zend_Table_Db find() function incorrectly.
To my knowledge, the only place that throws that exception is the find() function on Zend_Db_Table_Abstract.
Perhaps, inside the findAll() function (or in a function it calls) you're doing one of these:
$zendDbTable->find(1,2) //is looking for a compound key
$zendDbTable->find(array(1,2)) //is looking for two rows
When you really want the opposite.
I want to copy a record with all his relations.
I'm trying with:
$o = Doctrine::getTable('Table')->Find(x);
$copy = $object->copy();
$relations = $o->getRelations();
foreach ($relations as $name => $relation) {
$copy->$relation = $object->$relation->copy();
}
$copy->save();
This code doesn't works, but I think it's on the way.
I never could get the deep copy function to operate correctly.
I manually coded a deep copy function for one of my models like this
public function copyAndSave ()
{
$filters = array('id', 'created');
$survey = $this->copy();
$survey->Survey_Entries = new Doctrine_Collection("Survey_Model_Entry");
$survey->Assignment_Assignments = new Doctrine_Collection("Assignment_Model_Assignment");
$survey->Survey_Questions = new Doctrine_Collection("Survey_Model_Question");
$survey->save();
foreach ($this->Survey_Questions as $question)
{
$answers = $question->Survey_Answers;
$newQuestion = $question->copy();
$newQuestion->survey_surveys_id = $survey->id;
$newQuestion->save();
$newAnswers = new Doctrine_Collection("Survey_Model_Answer");
foreach($answers as $answer)
{
$answer = $answer->copy();
$answer->save();
$answer->survey_questions_id = $newQuestion->id;
$newAnswers->add($answer);
}
$newQuestion->Survey_Answers = $newAnswers;
$survey->Survey_Questions->add($newQuestion);
}
return $survey->save();
}
You can read about copy() here. It takes an optional parameter $deep:
$deep
whether to duplicates the objects targeted by the relations
So
$copy = $object->copy(true);
should do it.
Sorry if I'm resurrecting this thread...
I found myself in search of a solution recently where I needed to copy a record and retain the references of the original. A deep copy $record->copy(true) copies the references, which was no good for me. This was my solution:
$record = Doctrine_Core::getTable('Foo')->find(1);
$copy = $record->copy();
foreach($record->getTable()->getRelations() as $relation) {
if ($relation instanceof Doctrine_Relation_Association) {
$ids = array();
foreach ($relation->fetchRelatedFor($record) as $r) {
$ids[] = $r->getId();
}
$copy->link($relation->getAlias(), $ids);
}
}
if ($copy->isValid()) {
$copy->save();
}
Hope this helps :)
This is how i done, but some fix is needed.
$table = $entidade->getTable();
$relations = $table->getRelations();
foreach($relations as $relation => $data) {
try {
$entity->loadReference($relation);
} catch(Exception $e) {
die($e->getMessage());
}
}
I am using Symfony1.4.1 and that uses Doctrine 1.2.1 (I think).
I have been trying to make a function that did all the above myself, when I found one that already exists.
Try this in any function and look at the results:
$tmp=$this->toArray(TRUE);
var_dump($tmp);
$this->refreshRelated();
$tmp=$this->toArray();
var_dump($tmp);
$tmp=$this->toArray(TRUE);
var_dump($tmp);
exit();
I am going to try two different things:
A/ put $this->refreshRelated() into the constructor of all my model objects.
B/ write a function that takes an array depicting the object graph that I want populated. Calling the function refereshRelatedGraph($objectGraphArray). With the right structure of the array (having all the appropriate relation names at each level), I could control which relations get populated and which don't. One use for this is to populate only children, not parent relations. The other is for when a ERD/Schema/ObjectGraph has an element that is 'owned' by more than one object (many to many, other special circumstances that I have), I could control which side of the relationships get pre(non lazy) loaded.