I have this criteria propel.
public static function getPrenotazioniAttive($id_utente)
{
$c = new Criteria();
$c->add(self::USER_ID, 18793 );
$result = self::doSelect($c);
}
After that i add this:
echo $c->toString();
that shows:
Criteria: SQL (may not be complete): SELECT FROM `prenotazione` WHERE prenotazione.USER_ID=:p1 Params: prenotazione.USER_ID => 18793
Then i call the method before this way:
$prenotazioni = PrenotazionePeer::getPrenotazioniAttive($this->getUser());
var_dump($prenotazioni);
die("entro");
that creates/execute the SQL clause below.
SELECT IFNULL(SUM(prenotazione.VALUTAZIONE),0) AS somma,
COUNT(*) AS numero
FROM `prenotazione`
WHERE prenotazione.USER_ID=18793
that clause (if i go to phpmyadmin) retrives a row from the table.
My problem: var_dump($prenotazioni); just return null, any idea?
The getPrenotazioniAttive function you posted has no return clause. It should end with return $result; if you want to get the data outside the function.
Related
I tried to fetch data using joins and the data is repeating,
The controller code is:
public function searchjobs2()
{
//$id=$_SESSION['id'];
$lan = $_POST["picke"]; //var_dump($id);die();
$value['list']=$this->Free_model->get_jobs($lan);//var_dump($value);die();
$this->load->view('free/header');
$this->load->view('free/searchjobs2',$value);
}
And the model:
public function get_jobs($lan)
{
$this->db->select('*');
$this->db->from("tbl_work_stats");
$this->db->join("tbl_work", "tbl_work.login_id = tbl_work_stats.login_id",'inner');
$this->db->where("language LIKE '%$lan%'");
// $this->db->where('tbl_work_stats.login_id',$id);
$this->db->order_by('insertdate','asc');
$query=$this->db->get()->result_array();//var_dump($query);die();
return $query;
}
I have used
foreach ($list as $row){
...
}
for listing.
Using distinct will remove duplicate fields:
$this->db->distinct();
From what I can see, your query has ambiguity, and an error in the join statement, also your where like is part of the problem, I would recommend trying this even do there are some missing info, find out wich field you need to join from the second table.
public function get_jobs($lan){
$this->db->select("tbl_work_stats.*, tbl_work.fields");
$this->db->from("tbl_work_stats");
$this->db->join("tbl_work", "tbl_work_stats.login_id = tbl_work.login_id","inner");
$this->db->where("tbl_work.language LIKE", "%" . $lan . "%" );
$this->db->order_by("tbl_work_stats.insertdate","asc");
$query=$this->db->get()->result_array();
return $query;}
do you mean to join on login_id?
I am guessing that is the user logging in and it is the same for many entries of tbl_work_stats and tbl_work.
you didn't post your schema, , but login_id doesn't seem like right thing to join on. how about something like tbl_work.id = tbl_work_stats.tbl_work_id or similar?
also CI $db returns self, so you can do:
public function get_jobs(string $lan):array
{
return $this->db->select()
->from('tbl_work_stats')
->join('tbl_work','tbl_work.id = tbl_work_stats.work_id')
->like('language',$lan)
->order_by('insertdate')
->get()
->result_array();
}
I am currently using the code below in Prestashop to retrieve a cart id.
public function hookDisplayPDFInvoice($params) {
$order_invoice = $params['object'];
$id_order = (int)$order_invoice->id_order;
$sql = 'SELECT id_cart FROM '._DB_PREFIX_.'orders WHERE id_order="'.$id_order.'"';
//example id_cart
$id_cart = Db::getInstance()->execute($sql);
return $id_cart;
In database, there are id_cart and id_timeslot. Table is called ps_cart_timeslot.
I am quite stuck as I am baffled as to why the data return is 1 for any data I am retrieving.
$id_order is fine, it is returning the right value. Any data select I am querying will return 1.
Am I missing anything? Pardon me if this is a silly mistake.
Thank you.
For selects use Db::getInstance()->executeS($sql); or to get single value use Db::getInstance()->getValue($sql);
However since PS 1.6 you should be using query builder.
$query = new DbQuery();
$query->select('id_cart')
->from('orders')
->where('id_order = ' . (int)$id_order);
Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
// Or array of values
Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
execute() method returns only true or false while executeS() will return an array of select results (method can only be used for select queries) and getValue() will return the first value found in result.
In my Model, I wrote this function with MySQL raw query
function get_question_result($lr_id)
{
$raw_query = 'SELECT question_record.qr_id, LEFT(question.question, 50) as question,
question.correct_answer
FROM question_record
INNER JOIN question ON question.q_id = question_record.q_id
WHERE question_record.lr_id = '.$lr_id.' ';
$query = $this->db->query($raw_query);
$questionresult = $query->result_array();
return $questionresult;
}
It worked fine. It gave me the array I want. I continued my project.
Then suddenly I was curious to try it in CI Active Record Class.
function get_question_result($lr_id)
{
$this->db->select('question_record.qr_id, LEFT(question.question, 50) as question, question.correct_answer');
$this->db->from('question_record');
$this->db->join('question', 'question.q_id = question_record.q_id', 'inner');
$this->db->where('question_record.lr_id', $lr_id);
$result = $this->db->get()->result_array();
return $result;
}
It didn't work. It gave me this error
PHP Fatal error: Call to a member function result_array() on a non-object
Just out of curiosity, where did I do wrong?
Was it me writing it wrong or the result data structure with Active Record is just different?
'cause when I tried it again in Active Record without selecting this field
LEFT(question.question, 50) as question
It worked but it didn't give the field I want. Do you guys know why?
In your $this->db->select() call you need pass FALSE as second parameter so that active record will not try to add backticks ` for your columns in select statement
function get_question_result($lr_id)
{
$this->db->select('question_record.qr_id, LEFT(question.question, 50) as question, question.correct_answer',FALSE);
$this->db->from('question_record');
$this->db->join('question', 'question.q_id = question_record.q_id', 'inner');
$this->db->where('question_record.lr_id', $lr_id);
$result = $this->db->get()->result_array();
return $result;
}
According to docs
$this->db->select() accepts an optional second parameter. If you set
it to FALSE, CodeIgniter will not try to protect your field or table
names with backticks. This is useful if you need a compound select
statement.
$this->db->select();
I am trying create a query in one of my model.php
The query is like
public function getBedCategory(){
$query = (new \yii\db\Query())
->select('room_category')
->from('room_charges')
->innerJoin('patient_detail',
'patient_detail.bed_type = room_charges.room_name')
->where(['room_charges.room_name'=> 'patient_detail.bed_type',
'patient_detail.id'=> $this->id]);
$command = $query->createCommand();
$rows = $command->queryOne();
//var_dump($command);exit;
return $rows;
}
When doing a var_dump for $command I am getting the sql query like this:
SELECT `room_category` FROM `room_charges`
INNER JOIN `patient_detail` ON patient_detail.bed_type = room_charges.room_name
WHERE (`room_charges`.`room_name`=:qp0) AND (`patient_detail`.`id`=:qp1)
and on var_dump of $rows I am getting boolean:false
What I am doing wrong here and why I am getting this :qp0 and :qp1
Thanks for any suggestion.
As Tahir correctly stated :qp0 and :qp1 are place holders for the parameters. These are replaced by static values on execution of the query. Your problem is that patient_detail.bed_type should not be parametized. Your code should therefore read:
...
->where(['room_charges.room_name = patient_detail.bed_type',
'patient_detail.id'=> $this->id]);
For more information on where(), you can view the API page.
In my CodeIgniter project I'm getting the list of projects and successfully output them on a page. However, the data in one of the columns on that page should be retrieved from a different table in DB using the project ID. Could anybody help me to figure out how that can be done? Basically I need to make another query to that other table specifying the project id but don't actually know how to do that with CodeIgniter.
UPDATE
In the model I'm getting the list of projects with the following function:
function get_projects_list($page, $limit){
$sql = sprintf("SELECT * FROM Project WHERE deleted != 1 LIMIT %d, %d", ($page-1)*$limit, $limit);
$query = $this->db->query($sql);
return $query->result();
}
And in the controller I call the following function:
$projects_list = $this->Project_management_model->get_projects_list($curPage, self::$LIMIT_PER_PAGE);
$data['projects_list'] = $projects_list;
$data['cur_page'] = $curPage;
$data['page_count'] = $pageCount;
$this->load->view('project_management_view', $data);
And in the view I simply run on the $data with foreach and list the results in a table. In that table there's a column where I need to show a result from another table based on the ID of the project of that very row.
Thanks for helping.
You didn't mention whether you are using ActiveRecord or not. I am assuming that you are. I'll also guess that maybe what you need to do is use a JOIN.
If you were using straight SQL, you would do this using some SQL that might look something like this:
SELECT a.appointment_time, u.user_real_name FROM appointment a, site_user u WHERE u.site_user_id = a.user_id;
That would pull the user's name from the user table based on the user id in the appointment table and put it with the appointment time in the query results.
Using ActiveRecord, you would do something like this:
$this->db->select('appointment_time,user_real_name')->from('appointment')->join('site_user', 'site_user_id=appointment_user_id');
But why don't you tell us a little bit more about your question. Specifically, do you want this column in the other table to be related to the rows from the first table? If so, my JOIN suggestion is what you need.
I've actually found a way to do that with a custom helper. Creating a new helper and loading it in the controller gives an option to use the function from that helper in the view.
Thanks.
public function get data()
{
$this->db->flush_cache();
$query = $this->db->get('project_table');
$result = $query->result();
$data = array();
for ($i = 0;$i < count($result);$i++)
{
$data[$i] = $result[$i];
$data[$i]['project_data'] = temp($result[$i]->id);
}
return data;
}
private function temp($id = 0)
{
$this->db->flush_cache();
$this->where('id',$id);
$query = $this->db->get('project_table2');
$result = $query->result();
if (count($result) != 0)
return $result[0]->data;
}
you can do it by some thing like that,or you can use sub-query by query function of database.