I have a problem when I want to query table using ORM ,example I have article table with field id,author,text.
My code like this :
// Single where
$article = Model_Article::find()->where('id', 4);
print_r($article);
that't code will be fetch all field on table article, it's like select * from article where id = 4
Try Possibility
$article = Model_Article::find(null, array('id','title'))->where('id', 3);
the response is
object(Orm\Query)#89 (14) {
["model":protected]=>
string(10) "Model_Article"
["connection":protected]=>
NULL
["view":protected]=>
NULL
["alias":protected]=>
string(2) "t0"
["relations":protected]=>
array(0) {
}
["joins":protected]=>
array(0) {
}
["select":protected]=>
array(1) {
["t0_c0"]=>
string(5) "t0.id"
}
["limit":protected]=>
NULL
["offset":protected]=>
NULL
["rows_limit":protected]=>
NULL
["rows_offset":protected]=>
NULL
["where":protected]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(9) "and_where"
[1]=>
array(3) {
[0]=>
string(5) "t0.id"
[1]=>
string(1) "="
[2]=>
int(3)
}
}
}
["order_by":protected]=>
array(0) {
}
["values":protected]=>
array(0) {
}
}
that's is not return id or title field.
but when i'm try by adding get_one() method
$article = Model_Article::find(null, array('id','title'))->where('id', 3)->get_one();
id is return , but title is not and another field, i don't know why ?
Reference
ORM Discussion FuelPHP it's say ORM currently will be select all column, no plans to change that at the moment.
My Problem
Select Custom Field using ORM like this select id,owner from article where id = 4 it's will be return only id & owner, Is Possible to get that using ORM on FUELPHP ?
Do not use
Model_Article::find()->
but use
Model_Article::query()->
The first one works but is considered an error situation which might change in future versions.
As of version 1.4 the ORM supports partial selects, using
Model::query()->select('id', 'value')->
The second parameter of find() is an array of conditions for the find, such as 'where' or 'order_by' clauses. There is no support for selecting column names in this array.
The Orm\Model fetches all column because it can't deal with incomplete / partials objects.
If you want a custom query, don't use the ORM, use the query builder for that.
DB::select('id','title')->from(Model_Article::table())->where('id', 4);
if you are trying to find out result like query "select * from article where id = 4"
$article = Model_Article::find()->where('id', 4)->get_one();
print_r($article);
and one more concept you should understand
get_one() returns only one record (as a object ).
get() returns multiple record ( in the form of array of objects ).
and if your field is not displaying then check the model Model_Article .this problem may be occur when you have not declared fields in properties list.
Fuelphp has been release new version , you can see this documentation , in my case fuelphp version is 1.2 and cannot select custom field using orm.
select custom field using orm is available since version 1.4
http://fuelphp.com/docs/packages/orm/crud.html
// Find only some columns
Model_Article::query()->select('id', 'name');
Model_Article::find('all', array('select' => array('id', 'name')));
Related
I have been trying to solve this problem for a long time already. I tried searching for an answer in stackoverflow, google and other sources but I didn't manage to find something that could lead me to the solution of the problem. I really hope you can help me. Thanks in advance.
Im using Codeigniter 3
My query in codeigniter is as shown here bellow:
$this->db->select($fields)
->from("usuario as u")
->join("usuario_info as ui ", "ui.idusuario = u.idusuario","left");
$this->db->where("usua_url",$this->url_usuario);
return $this->db->get(); //last line
I do get results from the query with data from a record which has an id (in the database is a PK and can't be NULL
My compiled query from CI is:
SELECT * FROM `usuario` as `u` LEFT JOIN `usuario_info` as `ui` ON `ui`.`idusuario` = `u`.`idusuario` WHERE `usua_url` = 'heri'
And my result is the following after applying result_array method (I censored some field names and its values, you know..):
array(9) {
["idusuario"]=> NULL
["other_field1"]=> string(9) "censored"
["other_field2"]=> string(60) "censored"
["other_field3"]=> string(26) "censored"
["other_field4"]=> string(19) "censored"
`["other_field5"]=> string(7) "censored"
["other_field6"]=> string(4) "censored"
["other_field7"]=> NULL
["other_field"]=> NULL
}`
When I copy and paste exactly the same query into DataGrip or Workbench I get the same results except for idusuario, the results look like:
array(9) {
["idusuario"]=> 1
["other_field1"]=> string(9) "censored"
["other_field2"]=> string(60) "censored"
["other_field3"]=> string(26) "censored"
["other_field4"]=> string(19) "censored"
`["other_field5"]=> string(7) "censored"
["other_field6"]=> string(4) "censored"
["other_field7"]=> NULL
["other_field"]=> NULL
}`
Well after trying something I thought wouldn't make any difference I got to the solution.
I had a method like for the query like this:
public function selectPerfilInfo($fields = "*"){
$this->db->select($fields);
$this->db->from("usuario as u");
$this->db->join("usuario_info as ui ", "ui.idusuario = u.idusuario","left");
$this->db->where("usua_url",$this->url_usuario);
return $this->db->get();
}
I was calling it like this in my controller:
//before calling the method I already set the `url_usuario` attribute of my class
$this->my_controller->selectPerfilInfo();
I changed the line above to this:
$this->my_controller->selectPerfilInfo("u.idusuario, u.other_field1");
After passing the specific fields to the function it returns the right results.
Im not sure why it happens and this leaves me a lot of doubts because I have used the same structure for getting records from other tables with no problems.
I'd appreciate if any of you could give me any information about it. Thanks
I have a request with Kohana which returns an array, just like this :
$query = DB::select()
->from('environnement')
->where('utilisateur_id','=', 'd83fa9a71cc1c414011cc1dbeb270026')
->where('region_id','=', $region_selectionnee);
$id_env = $query->execute();
return $id_env->as_array();
I call the request in my controller and var_dump the result. I get :
array(1) {
[0]=> array(4) {
["id"]=> string(32) "d83fa9a71cc1c414011cc1de74270027"
["courant"]=> string(1) "0"
["region_id"]=> string(1) "1"
["utilisateur_id"]=> string(32) "d83fa9a71cc1c414011cc1dbeb270026"
}
}
Now, I want to get the id field of this array, but I don't know how to do. I tried with $id_environnement->id but it says
Trying to get property of non-object
I also tried with $id_environnement["id"], but it says :
Undefined index: id
Can somebody help me to get this id please ? Thanks in advance !
As you can see, the result is an array, in which is the object you want. So you have to use $id_environment[0]["id"] to get it.
Arr::path($id_environment, '0.id')
This method don't raise exception if array key not exists
You can also use:
$id = DB::select('id')
->from('environnement')
->where('utilisateur_id','=', 'd83fa9a71cc1c414011cc1dbeb270026')
->where('region_id','=', $region_selectionnee)
->limit(1)
->execute()
->get('id', false);
Where the second argument in get() is what you want returned if 'id' is not found (if you don't include a second argument, it will return null if 'id' is not found).
edit
You don't have to specify 'id' within select(), but if you do specify a field (or fields), get() can only retrieve from the specified field(s).
I'm trying to order a selection created by Doctrine\Common\Collections\Criteria,
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections
with multiple order attributes as specified possible (?) in the specification of criteria
/**
* #param array $orderings
* #return Criteria
*/
public function orderBy(array $orderings);
, however, the collection sorted only takes notice of my first entry in the sorting array. My array of $orderings looks like
array(5) { ["col1"]=> string(3) "ASC" ["col2"]=> string(3) "ASC" ["col3"]=> string(3) "ASC" ["col4"]=> string(3) "ASC" ["col5"]=> string(3) "ASC" }
Any ideas? The docs mentions andX() in the bottom of the page linked earlier but I can't figure out how I would use it in this case.
Cheers,
P
Seems to be a bug. Hopefully some genius will fix it soon :)
https://github.com/doctrine/doctrine2/issues/5540#issuecomment-163442353
I have this in my routes (only place it works...)
$constants = DB::table('constants')->get();
View::share('constants', $constants);
If in one of my views I var_dump constants I get this:
array(1) { [0]=> object(stdClass)#129 (3) { ["id"]=> string(1) "1" ["type"]=> string(13) "business_name" ["value"]=> string(17) "Project Framework" } }
I then want to do:
$business_name = $constants->business_name;
or
$business_name = $constants['business_name'];
How can I manipulate my array from the DB call to be able to use the constants variable like this?
Is there a way in Laravel to do this simpler / more efficient than looping through in a foreach and creating the array?
Assuming you have only one row in your constants you should use first() to retrieve a single record and not a collection:
$constants = DB::table('constants')->first();
Then you get a single object so you can use this notation to access the properties:
$business_name = $constants->business_name;
Edit
With your type-value structure I suggest using lists:
$constants = DB::table('constants')->lists('value', 'type');
This will create an array like this:
[
'business_name' => 'Project Framework',
'foo_type' => 'bar_value'
]
Usage
$business_name = $constants['business_name'];
How to update the post meta with array in wordpress.
For Example I tried this one to
$mail_list_array1 = get_post_meta($_POST['productid'], 'notify_emailsse');
$mail_list_array[] = $_POST['notifyaddress'];
$mail_list_arrays = array_merge($mail_list_array1, $mail_list_array);
update_post_meta($_POST['productid'], "notify_emailsse", $mail_list_arrays);
Now It returning the output as this way
array(1) {
[0]=> array(2) {
[0]=> array(2) {
[0]=> string(0) ""
[1]=> string(10) "t43tsdtret"
} [1]=> string(21) "weyriweyriynbdkxhfkds"
}
}
But I want this way
array(1) {
[0] => 'Email 1',
[1]=>'Email 2',
....}
How can i achieve that using post meta of wordpress. The reason what i am achieving for example:
If the user enter his email in the text field and i want to save the email in a one meta, same thing some other user also enter his email then that email should append as in common meta of wordpress. Shortly means saving multiple values into the same meta key.
Any Suggestion would be great.
Thanks,
vicky
I'm not sure about your purpose, but easiest will be to implode() arrays and then serialize() your post meta. After reading post meta do the opposite, to have data as an array: unserialize() and explode()