Normaly I call my database like this :
$data = array(
'one' => MyORM::paginate($this->per_page),
);
return View::make('project.index')->with($data);
But I also want to use an OrderBy so I can use:
$data = array(
'test' => MyORM::orderBy('date', 'DESC')->get(),
);
return View::make('project.index')->with($data);
But I don't know how I can "merge" the two codes?
I can also use :
$data = array(
'test2' => DB::table('martialp')
->orderBy('date', 'DESC')
->paginate(4)
);
But my class MyORM for exemple is useless with this previous code.
$data = MyORM::orderBy('date', 'DESC')->paginate(15);
return View::make('project.index', compact('data'));
Related
i´m traying to get of my DB to lowercase column data with model::where
this is my query:
$restaurants = Restaurant::where('name', 'LIKE', "%".$search."%")->get();
$results = [];
foreach ($restaurants as $r) {
$data = array(
'id' => $r->id,
'label' => $r->name,
'value' => $r->name,
'url' => $r->url,
);
array_push($results, $data);
}
i´m traying with this:
$restaurants = Restaurant::whereRaw('LOWER(name) = ?', [$search])->get();
but return empty results.
How i would can to do this?. In my database it´s all in uppercase i need search in uppercase or lowercase
Thanks for help
If you are using MySQL, MariaDB or PostgreSQL you could simply use a special version of LIKE that does not care for case: ILIKE:
$restaurants = Restaurant::where('name', 'ILIKE', "%".$search."%")->get();
$results = [];
foreach ($restaurants as $r) {
$data = array(
'id' => $r->id,
'label' => $r->name,
'value' => $r->name,
'url' => $r->url,
);
array_push($results, $data);
}
To be more efficient still you could use a collation on that field (or table, or database) on the database that is case insensitive, so every comparison ever made will be case insensitive (Beware that if you have and primary or unique key with an case insensitive collation you cannot have mulple entries with the same text only varying casing).
try this
$restaurants = Restaurant::whereRaw('LOWER(`name`) LIKE ? ',[trim(strtolower($search)).'%']);
if it's not work just add ->get() at last query
Can someone please point out my error, trying to sum a field(amount) using userId field?
https://fatfreeframework.com/3.6/mongo-mapper
Here is my code, this is returning me all documents matched with userId but not the sum.
$f3 = \Base::instance();
$mapper = new \DB\Mongo\Mapper($f3->get('MongoDB'),'transactions');
$filter = array('userId'=>'452');
$options = array(
array(
'group' => array(
'_id' => array('userId' => $userId),
'amount' => array('$sum' => 'amount')
)
)
);
$data = $mapper->find($filter, $options);
echo "<pre>";
print_r($data);
exit;
The mongo mapper uses the db.collection.group() method rather than the aggregation framework. Therefore you won't be able to call accumulators such as $sum.
Instead you must use the following syntax:
$group = [
'keys'=>['userId'=>1],
'initial'=>['sum'=>0],
'reduce'=>'function(obj,result){result.sum+=obj.amount;}',
'finalize'=>'function(result){}',
);
$data = $mapper->find($filter, ['group'=>$group]);
NOTE Please do not suggest using Eloquent, this is specifically for the Laravel query builder.
For performance reasons we are using Query Builder to retrieve results from a table:
DB::table('posts')->get();
If we then want to join a relation onto that query:
DB:table('posts')
->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
->get();
The results are merged into the array of each post:
[
'id' => 1,
'title' => 'My Blog Post',
'content' => '<h1>This is a post</h1><p>hello world</p>',
'post_author' => 'Billy',
'comment' => 'This is a comment',
'comment_author' => 'Andrew',
]
How can we have the joined results placed into a nested array? Such as:
[
'id' => 1,
'title' => 'My Blog Post',
'content' => '<h1>This is a post</h1><p>hello world</p>',
'post_author' => 'Billy',
'comment' => [
'id' => 22,
'comment' => 'This is a comment',
'comment_author' => 'Andrew',
],
]
Dont think its doable out of the box without Eloquent.
You can go the primitive route:
$results = DB:table('posts')
->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
->select('posts.*', 'comments.*', 'comments.id as comments_id')
->get();
foreach($results as &$result)
{
$result['comment'] = [
'id' => $result['comment_id'],
'comment' => $result['comment'],
'comment_author' => $result['comment_author']
];
unset($result['comment_author'], $result['comment_id']);
}
Since you work with DB facade and not Eloquent, and cannot use built-in with() method, you have to implement it yourself:
$posts = DB::table('posts')->get()->toArray();
$comments = DB::table('comments')->get()->toArray();
foreach($posts as &$post)
{
$post->comments = array_filter($comments, function($comment) use ($post) {
return $comment->post_id === $post->id;
});
}
return $posts;
If you want to get rid of post_id for comments entries, you can do:
$posts = DB::table('posts')->get()->toArray();
$comments = DB::table('comments')->get()->toArray();
foreach($posts as &$post)
{
$comments = array_filter($comments, function($comment) use ($post) {
return $comment->post_id === $post->id;
});
$post->comments = array_map(function ($comment) {
unset($comment->id);
return $comment;
}, $comments);
}
return $posts;
(I guess the runtime would be similar to with(), since after-all MySql does not provide this functionality out-of-the-box).
Here some new information:
$data= $DB->select("select posts.*,comments from posts left join comments on posts.id = comments.post_id");
I am not sure it is work or not but you can try
You can try with this
$data= $DB->select("select *,(select json_agg(datas) from (select * from comments where posts.id=comments.post_id) as datas) as comments from posts;");
But you may need to decode at comments as well
I would like to make an associative array using PHP for loop to use in Yii2 map() method.
The array will look like in bellow format-
$listArray = [
['id' => '1', 'name' => 'Peter/5'],
['id' => '2', 'name' => 'John/7'],
['id' => '3', 'name' => 'Kamel/9'],
];
The id and name will be changed through each iteration of the loop. Here, the name will always hold customized value after some calculation inside the loop.
Finally, the list will be used in map() method like as following
$listData=ArrayHelper::map($listArray,'id','name');
I can use map() method directly after using the Active Record to find the list array and then use that in map() method. But it does not a give me way to use custom value for the name attribute.
$listArray = UserList::find()
->where(['status' => 1])
->orderBy('name')
->all();
$listData=ArrayHelper::map($listArray,'id','name');
How can achieve this? Direct source code example would be really great for me.
Thanks in advance.
I'm assuming you want to query an ActiveRecord for data then transfer the data into a simple array.
$listData = [];
$listArray = UserList::find()
->where(['status' => 1])
->orderBy('name')
->all();
foreach($listArray as $user){
$customName = $user->name . $this->someCalculation();
$listData[] = ["id" => $user->id, "name" => $customName];
}
Or you could use the ArrayHelper class like this:
$listArray = UserList::find()
->where(['status' => 1])
->orderBy('name')
->all();
$listData = ArrayHelper::toArray($listArray , [
'app\models\UserList' => [
'id',
'name' => function ($listArray ) {
return $listArray->word . strlen($listArray->word); // custom code here
},
],
]);
I think the preferred way of doing this by defining custom calculation rule in UserList model as:
public function getCustomRuleForUser(){
// Do what ever you want to do with your user name.
return $this->name.'Your custom rule for name';
}
And use as:
$userList = UserList::find()->all();
$listData=ArrayHelper::map($userList,'id','customRuleForUser');
Now, you have your custom rule for username list in $listData.
$model_userprofile = UserProfile::find()->where(['user_id' => Yii::$app->user->id])->one();
$model_userprofile1 = UserProfile::find()
->select('user_id')
->where(['group_id' => $model_userprofile->group_id])->all();
$listData = [];
foreach($model_userprofile1 as $user){
$id = $user->user_id;
$listData[] = ["id" => $id];
}
$dataProvider = new ActiveDataProvider
([
'query' => User::find()
->select('id,username,email')
->Where(['id' => $listData])
->orderBy(['id' => SORT_DESC]),
'pagination' => ['pagesize' => 15]]);
return $this->render('index',['dataProvider'=> $dataProvider]);
I am using the findBy() method on a Doctrine repository:
$entities = $repository->findBy(array('type'=> 'C12'));
How can I order the results?
The second parameter of findBy is for ORDER.
$ens = $em->getRepository('AcmeBinBundle:Marks')
->findBy(
array('type'=> 'C12'),
array('id' => 'ASC')
);
$ens = $em->getRepository('AcmeBinBundle:Marks')
->findBy(
array(),
array('id' => 'ASC')
);
$cRepo = $em->getRepository('KaleLocationBundle:Country');
// Leave the first array blank
$countries = $cRepo->findBy(array(), array('name'=>'asc'));