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)
Related
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");
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 --')]) ?>
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.
I have the following repository:
class CustomerRepository extends EntityRepository
{
public function searchCustomer($criteria)
{
$q = $this->createQueryBuilder('c');
$q->join('TeamERPCustomerBundle:Company', 'o',
'WITH', 'c.company = o.id');
if (isset($criteria) and $criteria!=""){
$q->orWhere(sprintf("c.customer_name like '%s'", '%'.$criteria.'%'));
(...)
}
$q = $q->getQuery()->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
return $q;
}
}
I call it from the controller as follows:
$result = $this->getDoctrine()->getManager()
->getRepository('TeamERPCustomerBundle:Customer')
->searchCustomer($customerInfo);
It returns an array of elements as follows:
array (size=20)
0 =>
array (size=9)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
1 =>
array (size=2)
'id' => int 1
'company_name' => string 'Debswana' (length=8)
2 =>
array (size=9)
'id' => int 2
'customer_name' => string 'Kay' (length=3)
'address' => null
'postal_address' => null
'city_town_village' => null
'e_mail' => null
'land_line' => null
'cell_phone' => null
'fax' => null
3 =>
array (size=2)
'id' => int 3
'company_name' => string 'DTC' (length=3)
(...)
It gives one element of the array per object, but what I want it the classic mysql join; as result an array of elements of the same type. Something like mergin elements 1 and 2 together.
The other problem with this query is that is does not work like that classic join ether, it gives all the customers, but only the companies that are not repeated.
Can anyone help me to get a uniform array of elements?
Edit: I managed to find a solution:
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT c, i
FROM TeamERPCustomerBundle:Customer c
JOIN c.company i');
//$query->setParameter('id', '1'); With this is can add as many parameter as I need
return $query->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
This returned something like this:
array (size=11)
0 =>
array (size=10)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
'company' =>
array (size=2)
'id' => int 1
'company_name' => string 'Debswana' (length=8)
As you might have already appreciated this array is different from what I was getting before. so now I just have to go through the array and change it to what ever I want. for instance:
foreach ($result as $key => $value){
$result[$key]['company'] = $value['company']['company_name'];
}
Now we are talking. This Is kind of what I wanted:
array (size=11)
0 =>
array (size=10)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
'company' => string 'Debswana' (length=8)
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).