I've a method in my Controller:
function getSuggestions()
{
$query = Input::get('query');
$suggestions = Suggestion::where('word', 'LIKE', "$query%")->get()->toArray();
$return $suggestions;
}
and in Model I have:
public function toArray(){
$array = parent::toArray();
$array['matched'] = 'ok';
return $array;
}
How can I pass a variable to this toArray() method to append it to the Model?
Something like this: $array['matched'] = $query
I can't pass the $query directly to toArray($query). Any other way?
Just add the value to each matched array:
function getSuggestions()
{
$query = Input::get('query');
$suggestions = Suggestion::where('word', 'LIKE', "$query%")->get()->toArray();
return array_map(
function ($array) use ($query) { $array['matched'] = $query; return $array; },
$suggestions
);
}
Related
I have two controller functions both need one model function but when I pass parameters to single model function it shows me an error, I need help if there is another method to pass parameters
Controller
public function view_files()
{
$assignment_id='1255';
$data['files'] = $this->AdminModel->getRows111();
$this->load->view('admin/view_files', $data);
}
function download_test1(){
$id = $this->uri->segment(3);
if (empty($id)){
redirect(base_url());
}
$data = $this->AdminModel->getRows111($id);
$filename =$data['assignment_file_name'];
$fileContents = file_get_contents(base_url('upload/'.
$data['assignment_file_name']));
force_download($filename, $fileContents);
}
Model
public function getRows111($id=''){
$this->db->select('*');
$this->db->where('assignment_id','1255');
$this->db->from('tbl_assignments_files');
if($id){
$this->db->where('assignment_file_id',$id);
$query = $this->db->get();
$result = $query->row_array();
}else{
$query = $this->db->get();
$result = $query->result_array();
}
return !empty($result)?$result:false;
}
}
function 1
$data['files'] = $this->AdminModel->getRows111(assignment_id);
function 2
$data = $this->AdminModel->getRows111($id);
model by doing this, shows error
public function getRows111($id='',$assignment_id){
public function getRows111($id='',$assignment_id=""){
$this->db->select('*');
$this->db->from('tbl_assignments_files');
//if assignment_id is not empty it will add where clause to statement
if($assignment_id != ""){
$this->db->where('assignment_id',$assignment_id);
}
//if id is not empty it will add where clause to statement
if($id != ""){
$this->db->where('assignment_file_id',$id);
}
$query = $this->db->get();
$result = $query->result_array();
return !empty($result)?$result:false;
}
}
To call model function first parameter will be $id and second will be $assignment_id
To call model function by $id
$data = $this->AdminModel->getRows111($id);
OR
$data = $this->AdminModel->getRows111($id,'');
To call function by $assignment_id
$data = $this->AdminModel->getRows111("",$assignment_id);
To call function by both $id and $assignment_id
$data = $this->AdminModel->getRows111($id,$assignment_id);
Hope this helps!
you are using your function wrong,
base on the function getRows111 it does have 2 parameters need the $id and $assignment_id
proper used would be like this,
$this->AdminModel->getRows111($id, $assignment_id);
I suggest for your function to make it more flexible is this.
public function getRows111($array){
$id = $array[0];
$assignment_id = $array[1];
}
then call the function using with array parameter
$this->AdminModel->getRows111([$id, $assignment_id]);
to handle the array and you want it to be more flexible.
public function getRows111($array){
$id = isset($array['id']) ? $array['id'] : "";
$assignment_id = isset($array['assignment_id']) ? $array['assignment_id'] : "";
}
call the function like this
$this->AdminModel->getRows111(
['id' => $id,
'assignment_id' => $assignment_id
]);
// if you want just the id
$this->AdminModel->getRows111(['id' => $id]);
// or the the assignment id
$this->AdminModel->getRows111(['assignment_id' => $assignment_id]);
Customers_model
Class Customers_model extends BF_Model{
protected $table_name = 'customers';
protected $key = 'customer_id';
protected $date_format = 'datetime';
My query function in model
function get_customerlist()
{
$sql = $this->db->query('SELECT first_name, last_name , customer_id FROM customers ');
return $sql->result();
}
}`
Controller
public function listCustomer()
{
$this->load->model('customers_model'); // whatever you call it
$data['list'] = $this->customers_model->get_customerlist();
$this->load->view('myview', $data);
}
View
foreach($list as $value)
{
echo $value->first_name. '<br />'; output.
}
It can't show $list array from controller : Undefined list variable
In your Model Query method i made few changes,
function get_customerlist()
{
$selelct = array('first_name','last_name','customer_id');
return $this->db->select($select)
->from('customers')
->get()
->result();
}
In your Controller,
public function listCustomer()
{
$this->load->model('customers_model'); // whatever you call it
$list = $this->customers_model->get_customerlist();
$data['list'] = $list; //returned result is an array of object
$this->load->view('myview', $data);
}
In Your view,try this,
foreach((array)$list as $item)
{
echo $item->first_name. '<br />';
}
It should work.
How to pass the array into another page?
Example is I have the controller code below
$values = array(
'RECEIVER_PHYSICAL',
'RECEIVER_VIRTUAL',
'EMAIL_TEMPLATE'
);
foreach ($values as $id) {
$data['email'] = $this->mod_rewards->getEmail($id);
}
And this is the model that I want to pass the array
public function getEmail($id) {
$info = $this->core_db->select('*')
->from($this->schema.'.'.$this->v_system_config)
->where("key",$id)
->get();
return $return;
}
You can just add a new argument to the method getEmail.
$values = array(
'RECEIVER_PHYSICAL',
'RECEIVER_VIRTUAL',
'EMAIL_TEMPLATE'
);
foreach ($values as $id) {
$data['email'] = $this->mod_rewards->getEmail($id, $values);
}
add the argument like this:
public function getEmail($id, $values) {
though i think there must be something wrong with the design here.
Just rewrite your model function as:
public function getEmail($id,$array) {
//use array here
$info = $this->core_db->select('*')
->from($this->schema.'.'.$this->v_system_config)
->where("key",$id)
->get();
return $return;
}
While calling this model in controller you need to pass this array.
Hello any one can tell that how to load two function from model class in controller one method. I want to run multiple select quires in one page using codeigniter:
Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$select1 = $this->insertmodel->find($id);
$select2 = $this->insertmodel->detail_list();
$data = array();
$this->load->view('home/property_detail', ['select1'=>$select1], ['select2'=>$select2]);
//$this->load->view('home/property_detail', ['select2'=>$select2]);
}
Model
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list(){
$query1 = $this->db->query("select * from article");
return $query1->result();
}
In Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->find($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
In Model
public function find($id)
{
$query = $this->db->get_where('article', array('id' => $id), 0, 0)->get();
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
$result = $query1->result_array();
return $result;
}
In View
foreach ($select2 as $item) {
# your foreach lop goes here
}
As well check empty() before passing it to the foreach loop
As an alternative to #Rijin's useful answer newMethod() can make calls to the existing model methods. This might be useful if you don't want to break the interface already created for the model because you are using find($id) and detail_list() in other code.
Model:
public function find($id)
{
$query = $this->db->from('article')->where(['id' => $id])->get();
if($query->num_rows())
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
public function newMethod($id)
{
$result['select1'] = $this->find($id);
if($result['select1'] !== FALSE)
{
$result['select2'] = $this->detail_list();
return $result;
}
return FALSE;
}
Controller:
public function property_detail($id)
{
$this->load->model('insertmodel');
$data = $this->insertmodel->newMethod($id);
$this->load->view('home/property_detail', $data);
}
Model :
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
Controller :
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->newMethod($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
I have a form filter with few custom search fields.
In the addXXXQuery functions, I have to use join on a table which is the same for some fields.
Can I check if innerJoin for that particular table is already set in another addXXXQuery?
[EDIT] example:
public function addIsPaidColumnQuery(Doctrine_Query $query, $field, $values)
{
if ($values) {
$rootAlias = $query->getRootAlias();
$query->leftJoin($rootAlias.".Inscription i");
->andWhere("i.is_paid = ?", $values);
}
}
public function addActTypeColumnQuery(Doctrine_Query $query, $field, $values)
{
if ($values) {
$rootAlias = $query->getRootAlias();
$query->leftJoin($rootAlias.".Inscription i")
->leftJoin("i.Act a")
$query->addWhere("a.act_type_id = ?", $values);
}
}
Well, you can retrieve the $params from the query, and check if the join is already there, something like this:
public function addIsPaidColumnQuery(Doctrine_Query $query, $field, $values)
{
if ($values)
{
$rootAlias = $query->getRootAlias();
$leftjoin = $rootAlias.".Inscription i";
$params = $query->getParams();
if (!isset($params['join']) || (isset($params['join']) && !in_array($leftjoin, $params)))
{
$query->leftJoin($leftjoin);
}
$query->andWhere("i.is_paid = ?", $values);
}
}
You can see in the code, how a leftJoin in added.
I couldn't find a method, that would tell me if a join for a table is set. However the more I thought about the existence of such method, the less sence it made for it. So to check if I already used a join in the filter query I set a class variable $leftJoinInscriptionSet.
private $leftJoinInscriptionSet = false;
...
public function addIsPaidColumnQuery(Doctrine_Query $query, $field, $values)
{
if ($values) {
$this->setLeftJoinInscription($query);
$query->andWhere("i.is_paid = ?", $values);
}
}
...
private function setLeftJoinInscription(Doctrine_Query $query) {
if (!$this->leftJoinInscriptionSet) {
$rootAlias = $query->getRootAlias();
$query->leftJoin($rootAlias.".Inscription i");
$this->leftJoinInscriptionSet = true;
}
}