Saving a Multidimensional to Database - yii

Array
(
[name] => Array
(
[0] => 1
[1] => 1
)
[age] => Array
(
[0] => today
[1] => today
)
[grp] => Array
(
[0] => 2
[1] => 2
)
)
How I can save this into a table in yii. name, age and grp can have unlimited number of values.
table id, name, age and grp

if each array index is new record in your db , you can use this in your controller:
$lenth = count($yourArray['name']); // this line will return the count of records must be inserted to db table
for( $i = 0 ; $i < $lenth ; $i++ )
{
$newRecord = new ModelName;
$newRecord->name = $yourArray['name'][$i];
$newRecord->age = $yourArray['age'][$i];
$newRecord->grp = $yourArray['grp'][$i];
$newRecord->save();
}

Related

How to simplify my result in Cakephp 3

My code is:
$costSheetQuery = $this->CostSheets->find('all')->where(['id IN' => $criteria]);;
$costSheetData = $costSheetQuery->all();
I print the array $costSheetData and it gives me the following relust:
Cake\ORM\ResultSet Object
(
[items] => Array
(
[0] => App\Model\Entity\CostSheet Object
(
[id] => 5
[key_value] => {"sample_date":"10-03-2018","mh_sc":"mh-sc","buyer":"buyer","season":"season","style":"styel","style_detail":"Style Details","sample_img":[[""]],"fab_category":[["FABRIC CATEGORY","FABRIC CATEGORY","FABRIC CATEGORY","FABRIC CATEGORY","FABRIC CATEGORY","FABRIC CATEGORY"]],"patt":[["PATT","PATT","PATT","PATT","PATT","PATT"]],"size":[["SIZE","SIZE","SIZE","SIZE","SIZE","SIZE"]],"ship_date":[[{"year":"2018"},{"month":"01"},{"day":"01"}]],"piece":[["10","20","30","40","50","60"]],"fabric_usage":[["USAGE"]],"fabric_yds":[["yds"]],"fabric_body":[["Body","Body","Body","Body","Body","Body"]],"cm":[["embrotery"]],"cm_value":[[" 10 ","12.00","","15","",""]],"trims":[["thread"]],"trims_value":[["10","","10","20","",""]]}
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2018-03-10T16:47:52+00:00
[timezone] => UTC
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2018-03-10T16:47:52+00:00
[timezone] => UTC
[fixedNowTime] =>
)
[[new]] =>
[[accessible]] => Array
(
[key_value] => 1
[created] => 1
[modified] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => CostSheets
)
)
)
Whats hell is going on. I just start hating cakephp. My additional bullshit in my array? I also try to separate as : $items->costSheetData as item is an object of $costSheetData. But it gives me an arrow. Any idea?

Eloquent select from array in the same order

I have this eloquent call:
$array = [100, 200, 50, 3, 300];
$response = EloquentModel::whereIn('id', $array)->get();
but now I want the results to be order in the same order in which the array is ordered; so the result should be like:
0 => Obj(
[id] => 100
[name] => name100
)
1 => Obj(
[id] => 200
[name] => name200
)
2 => Obj(
[id] => 50
[name] => name50
)
3 => Obj(
[id] => 3
[name] => name3
)
....
The order is specified by how the $array variable has it's elements ordered...
Is it possible to do something like that in Eloquent?
Ok, so the solution is as follow:
$response = EloquentModel::whereIn('id', $array)->get()->sortBy(function($item, $index) use($array){
$arrayToSortBy = array_flip($array);
return $arrayToSortBy[$item->id];
});
Basically we can sort Eloquent:Collections response.
array_flip will flip keys with values, so that we can return the relevant number for ordering....

how to execute stored procedure in cakephp 3?

This is my function for login i call a stored procedure login for match user name and password, but it doesn't work for me it give me all the rows:
public function login($email,$password)
{
$consumers = TableRegistry::get('Consumers');
$result=$consumers->query("Call login('".$email."','".$password."')");
pr($result->toArray());die;
}
My stored procedure in phpmyadmin is below:
BEGIN
SELECT * FROM consumers WHERE email = email_id AND password = md_password;
END
When i am executing query it give me object but after converting this object into array it give me all the row of table.output is:
<pre class="pr">Array
(
[0] => Cake\ORM\Entity Object
(
[_properties:protected] => Array
(
[id] => 1
[name] => jeevan
[email] => j#gmail.com
[password] => asdf
[phone_no] => 8447726137
[ota] => cde
[status] => 0
[created_on] => Cake\I18n\FrozenTime Object
(
[date] => 2016-07-08 17:28:52
[timezone_type] => 3
[timezone] => UTC
)
[token_access] =>
[device_type] => 1
[push_id] => abc
[want_news] => 1
[postal_code] => 263136
[registration_type] => 1
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] =>
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_invalid:protected] => Array
(
)
[_accessible:protected] => Array
(
[*] => 1
)
[_registryAlias:protected] => Consumers
)
[1] => Cake\ORM\Entity Object
(
[_properties:protected] => Array
(
[id] => 2
[name] => jack
[email] => jack#gmail.com
[password] => 123
[phone_no] => 7409757656
[ota] => chb
[status] => 1
[created_on] => Cake\I18n\FrozenTime Object
(
[date] => 2016-07-20 06:10:14
[timezone_type] => 3
[timezone] => UTC
)
[token_access] => ghcvhgv
[device_type] => 0
[push_id] => hgnjh
[want_news] => 1
[postal_code] => 263136
[registration_type] => 1
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] =>
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_invalid:protected] => Array
(
)
[_accessible:protected] => Array
(
[*] => 1
)
[_registryAlias:protected] => Consumers
)
)</pre>
that mean the stored procedure not working, any idea really help me, thanks in advance!
You can call a stored procedure using the ConnectionManager's execute() method. It's important to remember to prepare the query so that you remove the risk of SQL injection:-
$this->connection = ConnectionManager::get('default');
$results = $this->connection->execute(
'CALL login(?, ?)',
[$email, md5($password)]
)->fetchAll('assoc');
When CakePHP runs execute() it will substitute the ? with the escaped and quoted values of $email and md5($password). For further details check out the official docs on Preparing a statement.
step 1:use the class as use Cake\Datasource\ConnectionManager;
step 2: then execute the procedure-
$this->connection = ConnectionManager::get('default');
$consumer = $this->connection->execute("CALL login('".$email."','".md5($password)."')")->fetchAll('assoc');
now it will give an array.
finally i got this answer in cakephp cookbook.

Error fetching data from database and undefine index

I'm passing value 13 to processpayment.php, but the function i call does not return any value to $req_que_info.
<a href="processpayment.php?id=13"</a>
processpayment.php?id=13:
include("database.php");
$queue_id=$_GET['id'];
$req_que_info = $database->getQueInfo($queue_id);
$count = count($req_que_info);
echo $req_que_info['queue_id'];
echo $count;
database.php:
function getQueInfo($queue_id){
$q = "SELECT * FROM ".TBL_QUEPAY." WHERE queue_id = ?";
$stmt = $this->connection->prepare($q);
$stmt->execute(array($queue_id));
$dbarray = $stmt->fetchAll();
return $dbarray;
}
because fetchAll returns a group of arrays like
Array
(
[0] => Array
(
[queue_id] => 13
[name] => foo
[age] => 99
)
[1] => Array
(
[queue_id] => 13
[name] => bar
[age] => 88
)
)
and fetch an (single) array like
Array
(
[queue_id] => 13
[name] => foo
[age] => 99
)
try
echo $req_que_info[0]['queue_id'];
may you use
foreach($req_que_info as $info)
echo $info . "<br />";
just see whats in the array.
print_r($req_que_info);

MongoDB - Search by insertion order (date) sub-value

Think you can give me your help on MongoDB .
My scheme is for example :
Array
(
[_id] => MongoId Object
(
[$id] => 538339ef5b17848a488b7e35
)
[group_name] => 'group 1'
[activity] => Array
(
[open] => Array
(
[company] => Array
(
[0] => Array
(
[name] => company 1
[date] => MongoDate Object
(
[sec] => 1400081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 2
[date] => MongoDate Object
(
[sec] => 1500081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 3
[date] => MongoDate Object
(
[sec] => 1600081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 4
[date] => MongoDate Object
(
[sec] => 1700081160
[usec] => 0
)
)
)
)
)
)
Array
(
[_id] => MongoId Object
(
[$id] => 538339ef5b178424498b7e34
)
[group_name] => 'group 2'
[activity] => Array
(
[open] => Array
(
[company] => Array
(
[0] => Array
(
[name] => company 3
[date] => MongoDate Object
(
[sec] => 1300081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 1
[date] => MongoDate Object
(
[sec] => 1600081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 2
[date] => MongoDate Object
(
[sec] => 1800081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 4
[date] => MongoDate Object
(
[sec] => 1900081160
[usec] => 0
)
)
)
)
)
)
And infact I would fill display the "groupname" only if such insertion order by DATE is "Company 1, Company 2, Company 3".
So normally it should show me in the example above only "Group 1", because the order is as required, while in the "Group 2" order starts with "Business 3, followed by Company 1. ... ".
--
For example if I'm looking like this:
[activity.open.company] => Array
(
[$in] => Array
(
[0] => company 1
[1] => company 2
[2] => company 3
)
)
This will highlight my whole world and not just the "Group 1"
--
Thank you sincerely for your help!
PS : Sorry for my english, I'm french.
When you query with an $in clause it basically becomes and $or on the items of the given array. The query you have written would match the two documents listed because they both have at least company 2 in them.