Get each Object id in Collection Form - php

I have a Collection of Colle Object in a form and I want to access id of each colle.
I tried :
$colles = $data['colles'];
Dump of $colles :
array (size=2)
1 =>
object(PACES\ColleBundle\Entity\Colle)[4156]
protected 'id' => null
protected 'nom' =>
object(PACES\ColleBundle\Entity\ColleQC)[4126]
private 'questions' =>
object(Doctrine\ORM\PersistentCollection)[4646]
...
protected 'id' => int 140
protected 'coefficient' => string '1.00' (length=4)
protected 'coefficient' => int 1
2 =>
object(PACES\ColleBundle\Entity\Colle)[4144]
protected 'id' => null
protected 'nom' =>
object(PACES\ColleBundle\Entity\ColleQC)[4583]
private 'questions' =>
object(Doctrine\ORM\PersistentCollection)[4592]
...
protected 'id' => int 150
protected 'coefficient' => string '1.00' (length=4)
protected 'coefficient' => int 1
For 1st object, I want getId() to get 'id' = 140 and for 2nd, 'id' = 150
This code returns null :
foreach ($colles as $colle) {
$idColle = $colle->getId();
}

If you look at the dump of $colles, you see that the 'id' of both 1 and 2 are "null", but it is the 'nom' collection (I think that's the collection) that has the Ids you are looking for.
Did you try:
$idColleNom = $colle->getNom()->getId();
I'm not certain of your setters and getters, but it might be something like that.

Related

Laravel CSV Import inserting with the same ID (Simple Import)

This is my store method which grabs the file and loops through each of the rows:
public function store()
{
$input = Input::file('statuses');
$filename = $input->getRealPath();
$i = 0;
$rows = Excel::load($filename, null, 'ISO-8859-1')->get()->toArray();
foreach($rows as $k => $row)
{
if(!isset($err)) {
if (!$this->repository->create($row))
$err = 'Error importing row ' + $i;
$i++;
}
}
if(isset($err)) {
Flash::error($err);
return Redirect::route('admin.importstatus.index');
}
Flash::success('Statuses Imported!');
return Redirect::route('admin.statuses.index');
}
In my repository, my create method looks like this:
public function create(array $data)
{
// Create the model
$model = $this->model->fill($data);
if ($model->save()) {
return $model;
}
return false;
}
Now, what appears to be happening when I import 6 rows only the final is actually getting inserted into the DB.
If I var_dump in my create method, I am being returned the following:
array (size=7)
'content' => string 'Imported two' (length=12)
'status' => float 0
'user_id' => float 1
'pinned' => float 0
'updated_at' => string '2015-06-28 16:13:22' (length=19)
'created_at' => string '2015-06-28 16:13:22' (length=19)
'id' => int 8
array (size=7)
'content' => string 'Imported three' (length=14)
'status' => float 0
'user_id' => float 1
'pinned' => float 0
'updated_at' => string '2015-06-28 16:13:22' (length=19)
'created_at' => string '2015-06-28 16:13:22' (length=19)
'id' => int 8
array (size=7)
'content' => string 'Imported four' (length=13)
'status' => float 0
'user_id' => float 1
'pinned' => float 0
'updated_at' => string '2015-06-28 16:13:22' (length=19)
'created_at' => string '2015-06-28 16:13:22' (length=19)
'id' => int 8
Notice how each of the ID's are all no. 8 (The next available row in the table). The tables ID is defo AUTO INCREMENT etc so no issues there, I guess its a logic issue? Any ideas?
Seems like you are using the same instance of a model over and over.
Try changing fill():
$this->model->fill($data);
with create():
$this->model->create($data);
With fill() you are only filling the already created model instance with some data. But if you use create() you are first creating a new instance (with a new id), then filling it with data and then saving it.
Important
When using create() you are also persisting it to the database, which means you don't have to save() it manually.

How to map a join to a join in Doctrine ORM native sql result set

How to map a join to a join in Doctrine ORM native sql result set
I want a result set with entities that have a join to a join. When dumping the result set I see that the join isn't correct.
The join in the second entity in the result set is joined to the first entity. And the join in the second entity is null.
But if I leave out the mapping of a field of the deepest join, the joins will be correct (except of the missing field).
relation
MyEntity ----> FooEntity ----> BarEntity
sql result
id some_field foo_id bar_id my_field
1 testa 123 456 test1
1 testb 8823 998 test2
mapping
$rsm->addEntityResult('MyEntity', 'a');
// first join
$rsm->addJoinedEntityResult('FooEntity', 'foo', 'a', 'foo');
// join to the first join
$rsm->addJoinedEntityResult('BarEntity', 'bar', 'foo', 'foo');
$rsm->addFieldResult('a', 'id', 'id')
->addFieldResult('a', 'some_field', 'someField')
->addFieldResult('foo', 'foo_id', 'id')
->addFieldResult('bar', 'bar_id', 'id');
print_r
// if I disable this addFieldResult, the joins are correct, but myField === null
//$rsm->addFieldResult('bar', 'my_field', 'myField')
object(stdClass)[352]
public '__CLASS__' => string 'FooEntity' (length=42)
public 'id' => int 123
public 'bar' =>
object(stdClass)[446]
public '__CLASS__' => string 'BarEntity' (length=49)
public 'id' => string '456' (length=2)
public 'myField' => null
object(stdClass)[352]
public '__CLASS__' => string 'FooEntity' (length=42)
public 'id' => int 8823
public 'bar' =>
object(stdClass)[446]
public '__CLASS__' => string 'BarEntity' (length=49)
public 'id' => string '998' (length=2)
public 'myField' => null
// if I enable this addFieldResult, the joins are incorrect
$rsm->addFieldResult('bar', 'my_field', 'myField');
object(stdClass)[352]
public '__CLASS__' => string 'FooEntity' (length=42)
public 'id' => int 123
public 'bar' =>
object(stdClass)[446] |
public '__CLASS__' => string 'BarEntity' (length=49) | THE WRONG BarEntity
public 'id' => string '998' (length=2) |
public 'myField' => null |
object(stdClass)[352]
public '__CLASS__' => string 'FooEntity' (length=42)
public 'id' => int 8823
public 'bar' => null | IT SHOULD BE HERE
Thanks for any help
Richard

array_rand() does not work on getResult()

I am trying to use array_rand() for random entity selection after getResult().
I expect to get an random entity but got int 1(int 2, int 3 etc) instead. Why?
$qb2->select('u')
->from('Application\Entity\User', 'u')
->where('u.usrlId = :sale');
if( !empty($usersIdListForExcluding) ) {
$qb2->andWhere($qb2->expr()->notIn( 'u.usrId', $usersIdListForExcluding ));
}
$qb2->setParameter('sale', 2);
$salesSet = $qb2
->getQuery()->getArrayResult();
var_dump(array_rand($salesSet));
var_dump($salesSet);
First var_dump gives:
int 1
Second one:
array (size=3)
0 =>
object(DoctrineORMModule\Proxy\__CG__\Application\Entity\User)[552]
public '__initializer__' =>
object(Closure)[528]
public '__cloner__' =>
object(Closure)[529]
public '__isInitialized__' => boolean true
private 'usrId' (Application\Entity\User) => int 2
private 'usrName' (Application\Entity\User) => string 'sales_test_1' (length=12)
private 'usrPassword' (Application\Entity\User) => string 'pass' (length=4)
private 'usrEmail' (Application\Entity\User) => string 's_1#gmail.com' (length=13)
...
1 =>
object(Application\Entity\User)[567]
private 'usrId' => int 6
private 'usrName' => string 'sales_test_3' (length=12)
private 'usrPassword' => string 'pass' (length=4)
private 'usrEmail' => string 's_3#gmail.com' (length=13)
private 'usrlId' => int 2
...
2 =>
object(Application\Entity\User)[526]
private 'usrId' => int 7
private 'usrName' => string 'sales_test_4' (length=12)
private 'usrPassword' => string 'pass' (length=4)
private 'usrEmail' => string 's_4#gmail.com' (length=13)
...
array_rand returns a random key (as opposed to a random value). If you want the value then you can access it via the key.
$random_key = array_rand($salesSet);
$random_value = $salesSet[$randomKey];

Double foreach getting only one

Hi how can I retrieve only 'fname' from this
array (size=1)
0 =>
array (size=6)
'tcldbid' => int 7
'tname' =>
object(TeamSpeak3\Helper\String)[252]
protected 'string' => string 'TEST' (length=4)
protected 'position' => int 0
'fcldbid' => int 2
'fname' =>
object(TeamSpeak3\Helper\String)[251]
protected 'string' => string 'Pinky' (length=5)
protected 'position' => int 0
'message' =>
object(TeamSpeak3\Helper\String)[256]
protected 'string' => string 'aaaaaa' (length=6)
protected 'position' => int 0
'timestamp' => int 1395092502
I am using foreach in laravel 4.1 blade
#foreach($p as $l)
#foreach($l as $key => $value)
{{$key . " " . $value}}</br>
#endforeach
#endforeach
This foreach is working but I am getting all data when I want to get fname or message only. Can anyone help me with that? I newbie who started learning php so example would be nice :)
No need to loop, you can just access the correct array element:
$fname = $p[0]['fname'];
$message = $p[0]['message'];

Building a form dynamically with Codeigniter and Active Record queries

Model
function get_prices()
{
$table_by_product = 'printer_businesscards'; //replace with URI Segment
//Get all the columns in the table set by the page URI of $table_by_product variable
$get_all_col_names = $this->db->list_fields($table_by_product);
//Loop through the column names. All names starting with 'O_' are optional fields for the
//current product. Get all Distinct values and create a radio button list in form.
foreach ($get_all_col_names as $key => $value) {
//get all o_types for the product by column name
if ($all_O_types = preg_match('/O_/', $value))
{
$O_types = array($value);
foreach ($O_types as $O) {
//echo $O;
$this->db->select($O);
$this->db->distinct();
$qO = $this->db->get($table_by_product);
$qO_Array = $qO->result_object();
}
}
//Get all x_types for the product by column name. All 'X_' types are specific product options.
//Create a dropdown menu with all DISTINCT product options.
if ($all_X_types = preg_match('/X_/', $value))
{
$X_types = array($value);
foreach ($X_types as $X) {
//echo $X;
$this->db->select($X);
$this->db->distinct();
$qX = $this->db->get($table_by_product);
$qX_Array = $qX->result_object();
}
}
}
return array($qX_Array,$qO_Array);
}
}
So each product has different options but all products options are prefixed by "X_" or "O_". I need to get the DISTINCT values of each COLUMN of "X_" and "O_" and in any VIEW I need to build a form with these values. Here is a look at the array:
array (size=3)
0 =>
object(stdClass)[19]
public 'X_SIZE' => string '1.75x3' (length=6)
1 =>
object(stdClass)[20]
public 'X_SIZE' => string '1.75x3.5(slim)' (length=14)
2 =>
object(stdClass)[21]
public 'X_SIZE' => string '2x3' (length=3)
array (size=3)
0 =>
object(stdClass)[17]
public 'X_PAPER' => string '14ptGlossCoatedCoverwithUV(C2S)' (length=31)
1 =>
object(stdClass)[18]
public 'X_PAPER' => string '14ptPremiumUncoatedCover' (length=24)
2 =>
object(stdClass)[24]
public 'X_PAPER' => string '16ptDullCoverwithMatteFinish' (length=28)
array (size=2)
0 =>
object(stdClass)[23]
public 'X_COLOR' => string '1000' (length=4)
1 =>
object(stdClass)[22]
public 'X_COLOR' => string '1002' (length=4)
array (size=4)
0 =>
object(stdClass)[20]
public 'X_QTY' => string '100' (length=3)
1 =>
object(stdClass)[21]
public 'X_QTY' => string '250' (length=3)
2 =>
object(stdClass)[17]
public 'X_QTY' => string '500' (length=3)
3 =>
object(stdClass)[19]
public 'X_QTY' => string '1000' (length=4)
array (size=3)
0 =>
object(stdClass)[25]
public 'O_RC' => string 'YES' (length=3)
1 =>
object(stdClass)[26]
public 'O_RC' => string 'NO' (length=2)
2 =>
object(stdClass)[27]
public 'O_RC' => string 'NA' (length=2)
My current MODEL is only returning X_QTY and O_RC to my view.
What am I doing incorrectly?
You are only returning the last result object of each; ie, you are setting
$qO_Array = $qO->result_object();
to a variable, and it should be:
$qO_Array[] = $qO->result_object();
to get all of them
BTW - do you have a specific reason for calling result_object() instead of the usual result() or result_array()? It's not necessarily wrong, but I wonder if you are doing it intentionally?

Categories