My array is :
$response = [
0 => [
'id' => 'US',
'text' => 'United States'
],
1 => [
'id' => 'CA',
'text' => 'Canada'
],
2 => [
'id' => 'FR',
'text' => 'France'
],
...
]
When I do a json_encode on it, for some reason the id value is 0 through the array:
{"id":0,"text":"United States"},
{"id":0,"text":"Canada"},
{"id":0,"text":"France"}
This only happens if the the column name is id as if json_encode forces ID to be numeric.
Any idea how to use a string in the id column ?
You need commas after all of your 'id' => 'XX' lines. After adding those and running json_encode it worked fine for me, the id's all kept their string values.
Try:
$response = [
0 => [
'id' => 'US',
'text' => 'United States'
],
1 => [
'id' => 'CA',
'text' => 'Canada',
],
2 => [
'id' => 'FR',
'text' => 'France'
]
];
var_dump(json_encode($response, 20));
RESULT:
{
"0": {"id": "US", "text": "United States"},
"1": {"id": "CA", "text": "Canada"},
"2": {"id": "FR", "text": "France"}
}
Related
I have some tables.
picture
people_ids type is TEXT. No foreign keys used.
main_id - is like main parent, one from PEOPLE table;
picture
I need SQL request to get result.
SELECT * from families ...
[
'main' => [
'name' => 'John',
'surname' => 'Brown'
],
'people' => [
[
'name' => 'John',
'surname' => 'Brown'
],
[
'name' => 'Merry',
'surname' => 'Brown'
],
[
'name' => 'Lizy',
'surname' => 'Brown'
]
]
]
Thanks for all, I'm dounded answer by the time)
Now I'm understanding that... that was maximally stupid idea. Learn more about SQL, table joining process and more.
I have one table like:
name
startDate
endDate
units
price
and I show them in a gridview for a given year as follows (index.php in views):
$gridColumns = [
'name',
[
'attribute' => 'january',
'format' => 'currency',
'value' => function ($data) {
if(date("m", strtotime($data["startDate"])) == 1) {
return ($data["price"]*$data["units"]);
}
else {
return "";
}
},
'options' => ['style' => 'max-width:20px;'],
'pageSummary' => true,
],
[
'attribute' => 'february',
'format' => 'currency',
'value' => function ($data) {
if(date("m", strtotime($data["startDate"])) == 2) {
return ($data["price"]*$data["units"]);
}
else {
return "";
}
},
'pageSummary' => true,
],
... until month 12
];
In my search model i have defined:
public $january, $february, $march, $april, $may, $june, $july, $august, $september, $october, $november, $december;
placed them as safe in rules and defining attributes in order to get grid header with sortable items
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 0,
],
'sort' => [
'defaultOrder' => [
'name' => SORT_ASC,
],
'attributes' => [
'name',
'units',
'price',
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december',
],
],
]);
but when i try to sort by one of the months it gives an error like:
exception 'PDOException' with message 'SQLSTATE[42S22]: Column not
found: 1054 Unknown column 'january' in 'order clause''
How could I sort by these calculated columns that doesnt exist in the database?
Thanks
I remember to have done this before, but now it does not work and I can't get it out.
[
'label' => 'Sex',
'attribute' => 'gan_sex',
'filter' => [
'1' => 'Male',
'2' => 'Female'
]
],
The output is
1
2
2
1
instead of
Male
Female
Female
Male
What is the problem now? I'd swear I used it just the same way but ...
I do it like this
[
'label' => 'Sex',
'attribute' => 'gan_sex',
'filter' => [
'1' => 'Male',
'2' => 'Female'
],
// translate lookup value
'value' => function ($model) {
$gender = [
'1' => 'Male',
'2' => 'Female'
];
return $gender[$model->gan_sex];
}
]
Possible values for gan_sex must be restricted to 1 and 2.
I would like to save data this way: 'Orders'->'OrdersFoods'->'OrdersFoodsChanges'. Where OrdersFoodsTable is a join table (Orders - Foods).
Why do I want to do so? Table OrdersFoods contains information about ordered foods (ids), but I would like to save additional data about every ingredient used for that food in particular order. That's why I've created additional table (OrdersFoodsChanges) with id from table OrdersFoods.
I can save data to a joined table but not to another table (OrdersFoodsChangesTable).
I've tried to do it the simplest way:
$order = $this->Orders->patchEntity($order, $this->request->data, [
'associated' => [
'Foods._joinData.OrdersFoodsChanges'
//'Foods.OrdersFoods.OrdersFoodsChanges'
]
but with no luck.
patchEntity object
object(Admin\Model\Entity\Order) {
'user_id' => (int) 1,
'city' => '',
'postal_code' => '',
'street' => '',
'house' => '',
'phone' => '',
'foods' => [
(int) 0 => object(Admin\Model\Entity\Food) {
'id' => (int) 15,
'name' => 'Royal',
'price' => (float) 15,
'category_id' => (int) 2,
'photo_id' => (int) 69,
'shop_id' => (int) 1,
'favorite' => false,
'vat' => (int) 23,
'visible' => true,
'position' => (int) 0,
'_joinData' => object(Admin\Model\Entity\OrdersFood) {
'quantity' => (int) 1,
'orders_foods_changes' => [
(int) 0 => object(Admin\Model\Entity\OrdersFoodsChange) {
'component_quantity' => (int) 25,
'component_id' => (int) 1,
'order_food_id' => (int) 1,
'type' => 'ADD',
'element_num' => (int) 1,
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'component_quantity' => true,
'component_id' => true,
'order_food_id' => true,
'type' => true,
'element_num' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Admin.OrdersFoodsChanges'
}
],
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'quantity' => true,
'orders_foods_changes' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Admin.OrdersFoods'
},
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'_joinData' => true
],
'[original]' => [
'_joinData' => [
'quantity' => '1',
'orders_foods_changes' => [
(int) 0 => [
'component_quantity' => (int) 25,
'component_id' => (int) 1,
'order_food_id' => (int) 1,
'type' => 'ADD',
'element_num' => (int) 1,
'id' => (int) 2
]
]
]
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Admin.Foods'
}
],
'price' => (float) 15,
'shop_id' => (int) 1,
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'user_id' => true,
'type' => true,
'city' => true,
'postal_code' => true,
'street' => true,
'house' => true,
'phone' => true,
'foods' => true,
'price' => true,
'shop_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Admin.Orders'
}
As I can see from sql log Cake does not even tries to save anything to a last table.
I would be grateful for any help.
So the main problem was that by default cake save method makes only one level association.
By default the save() method will also save one level of associations
When I've added associated it all went well.
if ($this->Orders->save($order, [
'associated' => ['Foods._joinData.OrdersFoodsChanges']
]))
I'm playing with RoseDB::Object on the employees test dataset, and for some reason, I can't get my foreign key relationships ('department' and 'employee') to work on the DeptEmp object. (Class structure below).
When I try $e->dept_emp->[0]->department, I get:
Can't locate object method "department" via package "My::FakeEmployees::DeptEmp"
Methods for the following relationships and foreign keys were deferred and
then never actually created in the class My::FakeEmployees::DeptEmp.
TYPE NAME
---- ----
Foreign Key department
Foreign Key employee
I'm sure I have something set up wrong in my class structure, but what?
CLASS STRUCTURE (some classes omitted for clarity):
I created the various objects using the instructions in the RDBO tutorial:
package My::FakeEmployees::Employee;
use strict;
use base qw(My::FakeEmployees::DB::Object);
__PACKAGE__->meta->setup(
table => 'employees',
columns => [
emp_no => { type => 'serial', not_null => 1 },
birth_date => { type => 'date', not_null => 1 },
first_name => { type => 'varchar', length => 14, not_null => 1 },
last_name => { type => 'varchar', length => 16, not_null => 1 },
gender => { type => 'enum', check_in => [ 'M', 'F' ], not_null => 1 },
hire_date => { type => 'date', not_null => 1 },
],
primary_key_columns => ['emp_no'],
'relationships' => [
'departments' => {
'type' => 'many to many',
'map_class' => 'My::FakeEmployees::DeptEmp',
},
'dept_emp' => {
'type' => 'one to many',
'class' => 'My::FakeEmployees::DeptEmp',
'column_map' => { 'emp_no' => 'emp_no' },
},
'dept_manager' => {
'type' => 'one to many',
'class' => 'My::FakeEmployees::DeptManager',
'column_map' => { 'emp_no' => 'emp_no' },
},
'salaries' => {
'type' => 'one to many',
'class' => 'My::FakeEmployees::Salary',
'column_map' => { 'emp_no' => 'emp_no' },
},
'titles' => {
'type' => 'one to many',
'class' => 'My::FakeEmployees::Title',
'column_map' => { 'emp_no' => 'emp_no' },
},
],
);
__PACKAGE__->meta->make_manager_class('employees');
1;
package My::FakeEmployees::DeptEmp;
use strict;
use base qw(My::FakeEmployees::DB::Object);
__PACKAGE__->meta->setup(
table => 'dept_emp',
columns => [
dept_no => { type => 'character', not_null => 1 },
emp_no => { type => 'integer', not_null => 1 },
from_date => { type => 'date' },
to_date => { type => 'date' },
],
primary_key_columns => [ 'emp_no', 'dept_no' ],
foreign_keys => [
department => {
class => 'My::FakeEmployees::Departments',
key_columns => { dept_no => 'dept_no' },
},
employee => {
class => 'My::FakeEmployees::Employees',
key_columns => { emp_no => 'emp_no' },
},
],
);
__PACKAGE__->meta->make_manager_class('dept_emp');
1;
package My::FakeEmployees::Department;
use strict;
use base qw(My::FakeEmployees::DB::Object);
__PACKAGE__->meta->setup(
table => 'departments',
columns => [
dept_no => { type => 'character', length => 4, not_null => 1 },
dept_name => { type => 'varchar', length => 40, not_null => 1 },
],
primary_key_columns => ['dept_no'],
unique_key => ['dept_name'],
'relationships' => [
'employees' => {
'type' => 'many to many',
'map_class' => 'My::FakeEmployees::DeptEmp',
},
],
);
__PACKAGE__->meta->make_manager_class('departments');
1;
Your foreign key has a typo:
foreign_keys => [
department => {
class => 'My::FakeEmployees::Departments',
That should be 'Department', not 'Departments'
It turned out to be a mistake in my code. These lines in DeptEmp.pm:
foreign_keys => [
department => {
class => 'My::FakeEmployees::Departments',
key_columns => { dept_no => 'dept_no' },
},
employee => {
class => 'My::FakeEmployees::Employees',
key_columns => { emp_no => 'emp_no' },
},
],
Have incorrect class names. It should be My::FakeEmployees::Employee and My::FakeEmployees::Department. Singular, not plural.