How to Pass multiple data array to view Codeigniter - codeigniter-3

I am new to Codeigniter and have two small question
1: How can I pass two table data array to view?
$data['customers']=$this->customer_model->get_all_customers();
//$products['product']=$this->customer_model->get_all_categories();
//$this->load->view("customer_view",$data);
$product['cats']=$this->customer_model->get_all_categories();
$this->loadViews("customer_view", $this->global, $data, $product);
But I am getting error
2: I am joining two tables to get two tables data but I am getting only one table data?
public function get_all_categories()
{
/*// Get Data from Two tables
$this->db->select('*');
$this->db->from('category');
$this->db->join('customers','customers.categoryID=category.categoryID','Inner');
$query=$this->db->get();
please help me to resolve.

This :
$query=$this->db->get();
Should be :
$query=$this->db->get()->result_array();

Here is solution for Passing multi variable to view
Controller
$data['customers']=$this->customer_model->get_all_customers();
$data['cats']=$this->customer_model->get_all_categories();
$data ['customers'] = $customers;
$data ['cats'] = $categories;
$this->loadViews("customer_view", $this->global, $data);
In view we will use $customers and $categories

Related

where Has Condition on a hasMany relationship in Laravel 5.8

I have a relationship in a model FeeModuleModel as shown below
public function heads()
{
return $this->hasMany('App\Models\FeeHeadModel','location_id','id');
}
and in my controller file i need to fetch only the values FeeModuleModel where the FeeHeadModel has type as unstructured
My controller code is as shown below
$modules = FeeModuleModel::where('vt_ay_id', '=', Session::get('sess_ay_id'))->with(['heads'=>function($q){
$q->where('type','=','unstructured');
}])->orderby('priority', 'asc')->get();
This fails with the following error
Call to a member function getRelationExistenceQuery() on array
What is the problem with my code and what I can do to solve it
Please change your code to this one
$modules = FeeModuleModel::with(['heads'=>function($q){
$q->where('type','=','unstructured');
}])->where('vt_ay_id', '=', Session::get('sess_ay_id'))->orderby('priority', 'asc')->get();

Laravel Eloquent - How to manually append accessor/attribute to result

I have an accessor/attribute ( public function getGalleryAttribute() ) that adds an array of images to the model
I used to eager load this with: protected $appends = ['gallery'] , but I got rid of it to be able to control when I want to append the gallery or not.
I can append the gallery to a single model:
$event = Event::find(126)->append('gallery');
But how do I manually append an accessor when there is more than one result?
This doesn't work:
$events = Event::all()->append('gallery');
return $events;
error:
BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::append does not exist
If you are using Eloquent 5.5 or later, you can use Collection::each to append to each item:
$events = Event::all()->each->append('gallery');
You should also look into Eloquent: API Resources to define alternative serialization options for the same model.

How to use one form to insert two entities in Symfony2?

I've found a technique to insert data in two entities with one form here:
http://forum.symfony-project.org/viewtopic.php?f=23&t=42011
My problem is that getData() return an array of objects and I can't use it with the persist() method because it expects an object. How can I fix it?
You might be looking for something like this:
$data = $form->getData();
foreach ($data as $object) {
$em->persist($object);
}
$em->flush();

Populate values to checkbox while editing in zend framework

I am trying to populate the values into check box. I want check box to be checked when there is value stored in database.
This is my code in form:
$form ['test_1'] = new Zend_Form_Element_Checkbox('test_1');
$form['test_1']->setLabel('test1')->setCheckedValue('1');
$form ['test_2'] = new Zend_Form_Element_Checkbox('test_2');
$form['test_2']->setLabel('test2')->setCheckedValue('2');
If there is value 1 in database i want first check box to be checked and if its 2 then 2nd checkbox needs to be checked.
What do i need to do in the controller.
Could anyone please help me on this issue.
The easiest way would be to fetch the values from the database as an array that maps to the form input elements, e.g. return a row like
array('test_1' => 'value of checkbox', 'test_2' => 'value of checkbox');
You could then simply call $form->populate($values) and let do Zend_Form do the setting, e.g. in your controller do
public function showFormAction()
{
$form = $this->getHelper('forms')->get('MyForm');
$data = $this->getHelper('dbGateway')->get('SomeTable');
$form->populate($data->getFormData());
$this->view->form = $form;
}
Note: the helpers above do not exist. They are just to illustrate how you could approach this. Keep in mind that you want thin controllers and fat models, so you should not create the form inside the controller, nor put any queries in there.

Zend Db Table Abstract fetchRow add new objects to it

I have a fetchRow, that is done like it:
$db = new Database();
$data = $db->fetchRow($db->select()->where('id = ?',$id));
Done it, I would like to retrieve all the files from the file table, like this:
$files = new Database();
$photos = $files->fetchAll($files->select()->where('id = ?',$data->id));
But it returns two different objects, how can I add it to only one object?
If I do:
$this->view->photos = $photos;
$this->view->data = $data;
It works, but how can I merge the photos inside the data?
Thanks and best regards.
If your table is a data set of files of different types then you need to control how they are hydrated - ie. what objects are returned from the query. To do this you need to make your own Rowset class for the table and set it as a propert of the Database class (assuming its a Zend_Db_Table).
In this rowset class you would examine the property of the row as its hydrated and then choose which class to use 'Data' or 'Photo'. To keep things consistent you will also need to modify the table class in order to handle hydration of a single record (ie. using a find method).
try
$db = new Database();
$data = $db->fetchRow($db->select()->where('id = ?',$id));
$files = new Database();
$photos = $files->fetchAll($files->select()->where('id = ?',$data->id));
array_push( $photos, $data);