Field inside Active Form with Array - php

I have a dropDownList with multiple select and when i goes to the actionUpdate, i wanna the content of dropDownList selected (correct content).
Array:
array (size=3)
0 =>
object(backend\models\AliHasAliPlanRef)[100]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 23
'ali_plan_ref_id' => int 50
'weight' => int 22
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 23
'ali_plan_ref_id' => int 50
'weight' => int 22
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
1 =>
object(backend\models\AliHasAliPlanRef)[105]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 24
'ali_plan_ref_id' => int 50
'weight' => int 33
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 24
'ali_plan_ref_id' => int 50
'weight' => int 33
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
2 =>
object(backend\models\AliHasAliPlanRef)[106]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 25
'ali_plan_ref_id' => int 50
'weight' => int 4
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=3)
'ali_s_id' => int 25
'ali_plan_ref_id' => int 50
'weight' => int 4
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
Actually i have this code (and works, but only for the first item selected of array):
<?=
$form->field($ali[0], 'ali_s_id', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'], ['prompt' => Yii::t('app', '-- Select --'),
])
?>
Example of the code above:
I wanna select all (correct) items seletected from DropDownList. I tried too:
<?=
$form->field($ali, 'ali_s_id[]', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'], ['prompt' => Yii::t('app', '-- Select --'),
])
?>
But i received an error:
Call to a member function isAttributeRequired() on array
Result expected:

you have to assign aleardy selected values properly. you can do this two ways.
$ali[0]->ali_s_id = [23, 24, 25];
or
$selectedValues = ["23" => ['selected'=>true], "24" => ['selected'=>true], "25" => ['selected'=>true] ];
<?= $form->field($ali[0], 'ali_s_id', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Category::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'],['options' => $selectedValues], 'prompt' => Yii::t('app', '-- Select --')]) ?>

Related

Replacing array keys with values within the same array

I need to replace the keys of my array with keys within the same array in PHP.
My array looks like this at the moment :
array (size=7)
0 =>
array (size=2)
'id' => int 978
'nbUnitsSold' => string '33.00000' (length=8)
1 =>
array (size=2)
'id' => int 979
'nbUnitsSold' => string '9.00000' (length=7)
2 =>
array (size=2)
'id' => int 980
'nbUnitsSold' => string '4.00000' (length=7)
3 =>
array (size=2)
'id' => int 981
'nbUnitsSold' => string '237.00000' (length=9)
4 =>
array (size=2)
'id' => int 983
'nbUnitsSold' => string '5.00000' (length=7)
5 =>
array (size=2)
'id' => int 984
'nbUnitsSold' => string '19.00000' (length=8)
6 =>
array (size=2)
'id' => int 991
'nbUnitsSold' => string '2.00000' (length=7)
I want it to look like this :
array (size=7)
978 =>
array (size=1)
'id' => int 978
'nbUnitsSold' => string '33.00000' (length=8)
979 =>
array (size=1)
'id' => int 979
'nbUnitsSold' => string '9.00000' (length=7)
980 =>
array (size=1)
'id' => int 980
'nbUnitsSold' => string '4.00000' (length=7)
981 =>
array (size=1)
'id' => int 981
'nbUnitsSold' => string '237.00000' (length=9)
983 =>
array (size=1)
'id' => int 983
'nbUnitsSold' => string '5.00000' (length=7)
984 =>
array (size=1)
'id' => int 984
'nbUnitsSold' => string '19.00000' (length=8)
991 =>
array (size=1)
'id' => int 991
'nbUnitsSold' => string '2.00000' (length=7)
Is there any function in PHP that could help without looping into the whole array ?
Thank you !
Just use array_column() with the third parameter as the column you want as the index...
$indexedArray = array_column($input,null,"id");

How to write a find query with condition in php-mongodb?

I'm having problem with find query in php-monogodb. find() without conditions works fine but when using condition inside find() method it won't give any results. How to solve this issue?
$connection = new Mongo();
$db = $connection->selectDB('db1');
$collection = $db->selectCollection('customers');
$cursor = $collection->find(array('CUSTOMER_ID' => $id));
$num_docs = $cursor->count();
if($num_docs > 0)
{
foreach($cursor as $obj)
{
echo 'Customer-Id: '.$obj['CUSTOMER_ID']."\n";
echo 'Customer Name: '.$obj['CUST_FIRST_NAME']." ".$obj['CUST_LAST_NAME']."\n";
echo 'Customer Email: '.$obj['CUST_EMAIL']."\n";
echo "\n\n\n";
}
}
After var_dump($cursor->explain()); it prints:
array (size=3)
'queryPlanner' =>
array (size=6)
'plannerVersion' => int 1
'namespace' => string 'db1.customers' (length=13)
'indexFilterSet' => boolean false
'parsedQuery' =>
array (size=1)
'CUSTOMER_ID' =>
array (size=1)
...
'winningPlan' =>
array (size=3)
'stage' => string 'COLLSCAN' (length=8)
'filter' =>
array (size=1)
...
'direction' => string 'forward' (length=7)
'rejectedPlans' =>
array (size=0)
empty
'executionStats' =>
array (size=7)
'executionSuccess' => boolean true
'nReturned' => int 0
'executionTimeMillis' => int 1
'totalKeysExamined' => int 0
'totalDocsExamined' => int 325
'executionStages' =>
array (size=14)
'stage' => string 'COLLSCAN' (length=8)
'filter' =>
array (size=1)
...
'nReturned' => int 0
'executionTimeMillisEstimate' => int 0
'works' => int 327
'advanced' => int 0
'needTime' => int 326
'needYield' => int 0
'saveState' => int 2
'restoreState' => int 2
'isEOF' => int 1
'invalidates' => int 0
'direction' => string 'forward' (length=7)
'docsExamined' => int 325
'allPlansExecution' =>
array (size=0)
empty
'serverInfo' =>
array (size=4)
'host' => string 'deadpool' (length=8)
'port' => int 27017
'version' => string '3.2.4' (length=5)
'gitVersion' => string 'e2ee9ffcf9f5a94fad76802e28cc978718bb7a30' (length=40)
Can you explain what is going wrong?
$cursor = $collection->find(array('CUSTOMER_ID' => $id)); where are you getting the id value from.
and try not using id as sometimes it has issues finding the value esp if $id is used elsewhere.

Why json_encode returns empty brackets?

var_dump of $array returns this :
array (size=3)
0 =>
object(frontend\models\Notifications)[101]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=5)
'id' => int 1
'created_on' => string '2015-11-12 12:12:15' (length=19)
'user_id' => int 1
'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
'is_seen' => int 0
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=5)
'id' => int 1
'created_on' => string '2015-11-12 12:12:15' (length=19)
'user_id' => int 1
'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
'is_seen' => int 0
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
1 =>
object(frontend\models\Notifications)[108]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=5)
'id' => int 2
'created_on' => string '2015-11-12 12:12:15' (length=19)
'user_id' => int 1
'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
'is_seen' => int 0
................................
................................
................................
But the json_encode($array) returns [{}, {}, {}].
What I attempted:
Tried changing the character encoding of the whole database to utf8_general_ci.
The character encoding is utf8_general_ci for my table and so is for my 'text' column of the table. What could be the issue?
The array you show has all the properties as private. this mean that this value are not available outside their class's scope.
you can look at this SO for some suggestion
Using json_encode on objects in PHP (regardless of scope)

PHP - How can I convert a ParseObject to a JSON?

I´m getting a ParseObject from a query and I need to convert it into json.
When I do a print_r($results) I get a lot of stuff but when I do $json = json_encode($results) and print it, the $json is empty.
Here is the var_dump of $results:
array (size=5)
0 =>
object(Parse\ParseObject)[12]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[15]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[15]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string 'mdHR3ZZo6z' (length=10)
private 'createdAt' =>
object(DateTime)[13]
public 'date' => string '2015-06-10 01:17:13' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[14]
public 'date' => string '2015-06-13 13:34:05' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
1 =>
object(Parse\ParseObject)[21]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[24]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[24]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string '3DK19BWOYY' (length=10)
private 'createdAt' =>
object(DateTime)[22]
public 'date' => string '2015-06-10 01:08:14' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[23]
public 'date' => string '2015-06-13 13:34:09' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
2 =>
object(Parse\ParseObject)[30]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[33]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[33]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string 'x94VIvFpdq' (length=10)
private 'createdAt' =>
object(DateTime)[31]
public 'date' => string '2015-06-10 01:07:38' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[32]
public 'date' => string '2015-06-13 13:34:11' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
3 =>
object(Parse\ParseObject)[39]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[42]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[42]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string 'z4vtVb9sdU' (length=10)
private 'createdAt' =>
object(DateTime)[40]
public 'date' => string '2015-06-10 01:13:05' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[41]
public 'date' => string '2015-06-13 13:34:07' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
4 =>
object(Parse\ParseObject)[48]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[51]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[51]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string 'LRsOGB7mjZ' (length=10)
private 'createdAt' =>
object(DateTime)[49]
public 'date' => string '2015-06-10 03:13:36' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[50]
public 'date' => string '2015-06-13 14:01:03' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
And here is after json_encode($results);
[{},{},{},{},{}]
Ideas?
Thank you for your time!

How could I expose data using var_dump on Eloquent ORM Laravel?

I'm new to php development and I want to use var_dump to debug my php program on the client side.
$test= Admin::where("EPANTHERID","=",$email);
var_dump($test);die();
On the client side, I receive this
bject(Illuminate\Database\Eloquent\Builder)[187] protected 'query' => object(Illuminate\Database\Query\Builder)[186] protected 'connection' => object(Illuminate\Database\MySqlConnection)[179] protected 'pdo' => object(PDO)[180] ... protected 'readPdo' => null protected 'reconnector' => object(Closure)[185] ... protected 'queryGrammar' => object(Illuminate\Database\Query\Grammars\MySqlGrammar)[181] ... protected 'schemaGrammar' => null protected 'postProcessor' => object(Illuminate\Database\Query\Processors\MySqlProcessor)[182] ... protected 'events' => object(Illuminate\Events\Dispatcher)[14] ... protected 'paginator' => object(Closure)[184] ... protected 'cache' => object(Closure)[183] ... protected 'fetchMode' => int 8 protected 'transactions' => int 0 protected 'queryLog' => array (size=0) ... protected 'loggingQueries' => boolean true protected 'pretending' => boolean false protected 'database' => string 'xinwen_development' (length=18) protected 'tablePrefix' => string '' (length=0) protected 'config' => array (size=9) ... protected 'grammar' => object(Illuminate\Database\Query\Grammars\MySqlGrammar)[181] protected 'selectComponents' => array (size=11) ... protected 'tablePrefix' => string '' (length=0) protected 'processor' => object(Illuminate\Database\Query\Processors\MySqlProcessor)[182] protected 'bindings' => array (size=5) 'select' => array (size=0) ... 'join' => array (size=0) ... 'where' => array (size=1) ... 'having' => array (size=0) ... 'order' => array (size=0) ... public 'aggregate' => null public 'columns' => null public 'distinct' => boolean false public 'from' => string 'ADMIN' (length=5) public 'joins' => null public 'wheres' => array (size=1) 0 => array (size=5) ... public 'groups' => null public 'havings' => null public 'orders' => null public 'limit' => null public 'offset' => null public 'unions' => null public 'lock' => null protected 'backups' => array (size=0) empty protected 'cacheKey' => null protected 'cacheMinutes' => null protected 'cacheTags' => null protected 'cacheDriver' => null protected 'operators' => array (size=19) 0 => string '=' (length=1) 1 => string '<' (length=1) 2 => string '>' (length=1) 3 => string '<=' (length=2) 4 => string '>=' (length=2) 5 => string '<>' (length=2) 6 => string '!=' (length=2) 7 => string 'like' (length=4) 8 => string 'not like' (length=8) 9 => string 'between' (length=7) 10 => string 'ilike' (length=5) 11 => string '&' (length=1) 12 => string '|' (length=1) 13 => string '^' (length=1) 14 => string '<<' (length=2) 15 => string '>>' (length=2) 16 => string 'rlike' (length=5) 17 => string 'regexp' (length=6) 18 => string 'not regexp' (length=10) protected 'model' => object(Admin)[178] protected 'table' => string 'ADMIN' (length=5) protected 'fillable' => array (size=1) 0 => string 'EPANTHERID' (length=10) protected 'connection' => null protected 'primaryKey' => string 'id' (length=2) protected 'perPage' => int 15 public 'incrementing' => boolean true public 'timestamps' => boolean true protected 'attributes' => array (size=0) empty protected 'original' => array (size=0) empty protected 'relations' => array (size=0) empty protected 'hidden' => array (size=0) empty protected 'visible' => array (size=0) empty protected 'appends' => array (size=0) empty protected 'guarded' => array (size=1) 0 => string '*' (length=1) protected 'dates' => array (size=0) empty protected 'touches' => array (size=0) empty protected 'observables' => array (size=0) empty protected 'with' => array (size=0) empty protected 'morphClass' => null public 'exists' => boolean false protected 'eagerLoad' => array (size=0) empty protected 'macros' => array (size=0) empty protected 'onDelete' => null protected 'passthru' => array (size=12) 0 => string 'toSql' (length=5) 1 => string 'lists' (length=5) 2 => string 'insert' (length=6) 3 => string 'insertGetId' (length=11) 4 => string 'pluck' (length=5) 5 => string 'count' (length=5) 6 => string 'min' (length=3) 7 => string 'max' (length=3) 8 => string 'avg' (length=3) 9 => string 'sum' (length=3) 10 => string 'exists' (length=6) 11 => string 'getBindings' (length=11)
It seems like Eloquent has some mechanism which prevents me from accessing data using var_dump. How could I solve this problem or could anyone show me a better way to debug my php scripts? Thank you.
You should use:
$test= Admin::where("EPANTHERID","=",$email)->get();
or
$test= Admin::where("EPANTHERID","=",$email)->first();
if EPANTHERID is unique and then use var_dump on result.
That's because without get() or first() (and some other methods) you have in your variable only object that holds your query and not the result of running the query (the query in your code has not been launched yet).

Categories