i'm very new on cakephp.I have 35 tables in my data base and want to edit default index.ctp views to get recent entry first and also in other search results I want to put recent entries first. So, I'm try to edit find() function using 'beforeFind()' callback. I wrote following function and put it to 'AppController', but it didn't work. Is there any error in this code or I put it on wrong place.Does anyone help me to find mistake ? Thanks
function beforeFind($queryData) {
if (!isset($queryData['order'])) {
$queryData['order'] = array();
}
$queryData['order'][$model->alias.'.id'=> 'DESC'];
return $queryData;
}
Use cakephp model attribute
The default ordering of data for any find operation. Possible values include:
$order = "field"
$order = "Model.field";
$order = "Model.field asc";
$order = "Model.field ASC";
$order = "Model.field DESC";
$order = array("Model.field" => "asc", "Model.field2" => "DESC");
refer cakephp documentation
So in AppModel just define the following
public $order = "id desc";
Related
I am trying to duplicate data with a click of a button, in better terms, trying to reorder a previous order. This is my code
$order = Order::find($id);
$order_details = OrderDetail::where('order_id', $id)->get();
$reorder = $order->replicate();
$reorder_details = $order_details->replicate();
$reorder->save();
$reorder_details->save();
The $order data replicates fine, however the $order_details data doesnt, as I get this error Method Illuminate\Database\Eloquent\Collection::replicate does not exist.
Is there a way to duplicate without using replicate()?
It's because $order = Order::find($id); returns the first instance (a model) and $order_details = OrderDetail::where('order_id', $id)->get(); returns a collection. Just have to change it to $order_details = OrderDetail::where('order_id', $id)->first(); and it will work fine.
To handle multiple order details:
$order_details = OrderDetail::where('order_id', $id)->get()->each(function($item) use($reorder){
$newItem = $item->replicate();
$newItem->order_id = $reorder->id; //If needed, be sure to pass $order if you do
$newItem->save();
});
You can use the __clone() method which you can implement inside the order class,
Then you can use it like this
$order = Order::find($id);
$newOrder = clone $order;
$newOrder->save();
recently I'am trying to make my api filtering work. I need to filter my products like this: http://localhost/search?feature_id=1,2,3,4,5...
Everything is fine if I'm sending only 1 id. But how to make it work in this way?
This is my controller:
public function search2(\Illuminate\Http\Request $request) {
$query = DB::table('tlt_product_features');
if ($request->has('feature_id') ) {
$query = $query->whereIn('feature_id', [$request->get('feature_id')]);
}
$products = $query->get();
return response()->json([
'products' =>$products
]);
}
Use explode() to make arrays of id.
$ids = explode(",",$request->get('feature_id'));
$query = $query->whereIn('feature_id', $ids);
To get an array out of the box on the Laravel / Lumen side, you have to send the array this way :
http://localhost/search?feature_id[]=1&feature_id[]=2&feature_id[]=3...
In a weak typed languages like PHP, the [] is actually being used as an internal work around in order to be able to get multi valued parameters. You could also specify an index :
http://localhost/search?feature_id[0]=1&feature_id[1]=2&feature_id[2]=3...
You could then use in you controller :
if ($request->filled('feature_id')) {
// You could also check that you have a php array : && is_array($request->input('feature_id'))
// And that it's not an empty array : && count($request->input('feature_id'))
$query = $query->whereIn('feature_id', $request->input('feature_id'));
}
I'm new to laravel, so I'm searching for how to send a specific id to the controller in order to get data from another table ?
For example
while($shipment = sqlsrv_fetch_array($get_customer_rule,SQLSRV_FETCH_ASSOC)) {
//display some data ......
// inside the loop i will have another query to get data from another table has //relation with shipment table
$id = $shipment['id'];
$customer = "SELECT * FROM [ccctadm].[customer] WHERE id = '$id' ";
$get_customer_info = sqlsrv_query($conn, $customer);
$get_customer_id = sqlsrv_fetch_array($get_customer_info,SQLSRV_FETCH_ASSOC);
$customer_id = $get_customer_id['customerid'];
}
I can't write query in while loop in laravel, so how can I pass shipment id to the controller so I can get customer data related to the shipment
Since you are new to Laravel, maybe you should learn the Laravel way first. Watch this video on how to work with Eloquent and perhaps every other video in that series. https://laracasts.com/series/laravel-from-scratch-2017/episodes/7
Once you get your head around that, you will be able to rewrite your query as
$shipments = Shipement::all();
foreach( $shipments as $shipment ) {
$customer = $shipment->customer;
$customer_id = $customer->id;
}
Even better when you get a bit further with laravel and be able to work with eager loading, you will just do
$shipments = Shipment::with('customer')->get();
And in your view
#foreach($shipments as $shipment)
Customer ID is : {{ $shipment->customer->id }}
#endforeach
You have decided you to work with Laravel. Take advantage of it. It will make everything easier and speed up your development process.
If you want to stick to your raw SQL queries, you can use the query builder
$result = DB::select("SELECT * FROM [ccctadm].[customer] WHERE id = ?", [$id]);
And work with the result
I'm new to Doctrine, and I just could not find a way to get the total number of results when using limit with Criteria (via setMaxResults function) in the EntityRepository::matching method.
In my repository (not an extend of EntityRepository), I'm using the following (I know this is not the optimal code, it is used just to learn Doctrine):
public function getAll($query = null) {
if ($query instanceof Criteria) {
$users = $this->em->getRepository('App\Entities\User')->matching($query)->toArray();
} else {
$users = $this->em->getRepository('App\Entities\User')->findAll();
}
return $users;
}
Now lets say that the Criteria is defined like so:
$query = Criteria::create();
$query->where(Criteria::expr()->contains('username', 'ron'));
$query->setMaxResults(10);
And there are actually more than 10 users that match that.
How can I get the total number of the users that match the criteria?
If you set maxResults to 10, you get 10 results ;).
Why don't you call getAll() to get all results and apply the MaxResults later?
//search for Ron's
$query = Criteria::create();
$query->where(Criteria::expr()->contains('username', 'ron'));
//check how many Ron's your database can find
$count = $repo->getAll($query)->count();
//get the first 10 records
$query->setMaxResults(10);
$users = $repo->getAll($query);
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.