getting a distinct value from db through cake-php - php

Hi i want to get distinct values from a particular column from cakephp.
This is what i have tried:-
$new_data =$this->Event->findAll(null, 'DISTINCT events.source');
Event- is the name of my model.
events- is my table name in db.
Source:- is the column name from which i want to fetch values.
I dnt knw what is wrong in the query, can anyone help me out

findAll is not actually a cakephp method. What you want to do is:
$this->Event->find('all', array('fields' => 'DISTINCT Event.source'));
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-all

$this->Event->find('all', array('group' => 'Event.source'));

$this->Event->find('all', array('group' => 'Event.source'));

Related

multiple value insert using saveField function or other function in cakephp

I insert multiple values into my database using saveField function. Here is my code.
$this->Fuel->saveField(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
);
It's not a form value. Value automatically insert when the page is refreshed.
It's only inserts first value 'device_id', $int_fuel_func[0],. Other two values are not insert into my database. Any one suggest me how to insert these values.
Thanks in advance
Cake version 2.7.5
You can simply do this as well.
$data = array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
);
$this->Fuel->save($data);
Also try this blog tutorial (cakephp blog tutorial) if you haven't been there before to be more familiar with these things.
My id is not auto Increment it is defined as varchar. Time is not include there also. I got my answer. I use it and it works.
$this->Fuel->set(
array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
)
);
$this->Fuel->save();
saveField function is only for updating one field! use updateAll and pass an array to it :
$this->Fuel->updateAll(
array(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
),
array('Fuel.id' => 234) // conditions for the Fuel you want to update
);

Laravel update Issue

Here is my code -
$updatecompany = DB::table('Companies')
->where('ID', (int)$companyid)
->update(array(
'CompanyName' => $companyname,
'CompanyAddress' => $companyaddress,
'CompanyEmail' => $companyemail,
'ContactName' => $contactname,
'CompanyCity' => $companycity,
'CompanyState' => $companystate,
'CompanyZip' => $companyzipcode,
'CompanyPhone' => $companyphone,
));
$updatecompany is always 0. What might be the problem?
One of most possible reasons is that you are updating with the same data in the database.
There needs one out of the box solution, of course if you can do it.
So, no rows are updating, even if the SQL is correct.
Here is my suggestion:
Add a new column updatedOn in DB Table Companies.
The type should be TIMESTAMP and add attribute ON UPDATE CURRENT_TIMESTAMP.
This way you will always get row affected and hence you get return value other than 0.
You don't need to cast $companyId to an integer there. It does not help Laravel's query builder.
Use dd($companyId) and dump the variable before you run the query and find out what it is.

get a single value from a table using the query() method inside a model

I have a very complex setup on my tables and achieving this via any of the find() methods is not an option for me, since I would need to fix relationships between my tables and I don't have the time right now, so I'm looking for a simple fix here.
All I want to achieve is run a query like this:
SELECT MAX( id ) as max FROM MyTable WHERE another_field_id = $another_field_id
Then, I need to assign that single id to a variable for later use.
The way I have it now it returns something like [{{max: 16}}], I'm aware I may be able to do some PHP on this result set to get the single value I need, but I was hoping there was already a way to do this on CakePHP.
Assuming you have a model for your table and your are using CakePHP 2.x, do:
$result = $this->MyTable->field('id', array('1=1'), 'id DESC');
This will return a single value.
see Model::field()
This example is directly from the CakePHP documentation. it seems you can use the find method of a model to get count
$total = $this->Article->find('count');
$pending = $this->Article->find('count', array(
'conditions' => array('Article.status' => 'pending')
));
$authors = $this->Article->User->find('count');
$publishedAuthors = $this->Article->find('count', array(
'fields' => 'DISTINCT Article.user_id',
'conditions' => array('Article.status !=' => 'pending')
));

why not insert query is working in codeigniter when ever we use right syn.?

why not insert query is working whenever i am using right syn.,i have used this type syntax in my other function of same controller.
code
$reviewData = $this->input->post('reviewData');
$id=1;
$rdaraaa = array(
'id' => $id,
'content' => $reviewData
);
$this->db->insert('reviews', $rdataaa);
please help me
Your variable name is wrong.It should be
$this->db->insert('reviews', $rdaraaa);
You have given wrong array $rdataaa instead of $rdaraaa.And makesure that id,content are the column names in your table reviews.
remove this code print_r($rdaraaa); die; and it should be
$this->db->insert('reviews',$rdaraaa)

find by column name cakePHP

Hello I have been trying to understand how to get data from model by the name of field. I am using cakePHP, and I need to retreive a column's data from a table. The syntax is
> "select name from permissions"
So I tried to find out on book.cakephp.org, so I got the field function, but that only gives me the first value, while I have more than one values for this.
I tried do a
$this->Model->find(array('fields'=>'Model.fieldName'));
but I understood that the syntax itself is flawed.
Can somebody let me know what is the method to query based on column name.
$this->Model->find(array('fields'=>'Model.fieldName'))
You forgot the array function. Also:
$this->Model->find(array('fields'=>array('Model.fieldName')))
will work.
findAllBy will find all records based on the field name.
$this->Model->findAllBy<fieldName>(string $value, array $fields, array $order, int $limit, int $page, int $recursive);
For eaxample:
$this->Permission->findAllByName('Some Name');
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#findallby
Found it... hope it will help someone.
$workshop_lists = ClassRegistry::init('Workshop')->find('all',array(
'fields'=>array('user_id', 'title')
),
array(
'conditions' => array('user_id' => $this->Auth->user('id')),
'group' => 'Workshop.user_id',
'order' => 'posted DESC',
));
There is no way you can query out based on column name using one of the cake methods. You have to use the query method.
Syntax: $this->Model->('Select columnname from table');
$this->Model->find('all',array('fields'=>array('Model.fieldName')))
it works for me everytime.
If I understood well and you want not only 1 value but the whole values in the column 'name' from the table 'permissions'. In that case you could use:
$this->Model->find('list',$params);
(see explanation for 'find' here)
for the '$params' part you would use:
$params=array('fields'=>array('name'));
or putting all in a single line:
$arrayOfNames= $this->Model->find('list',array('fields'=>array('name')));
This will give you an array '$arrayOfNames' wich key is the 'id' (primary key) in 'permissions' table and wich value is the corresponding name in the field 'name' from the same table. This is the array would be something like:
'id'=>'name'
[23]=>'name1'
[28]=>'name2'
[29]=>'name3'
............
very much like I think you want. Hope it helps.
$this->Model->find('list', ['valueField' => 'fieldName']);

Categories