Kohana framework with ORM create custome where - orm

I am using Kohana framework 3, and I need to create a query
I need add one where part only if variable is true, otherwise I don't need one WHERE part.
How add php if script in query?
What I need:
ORM::factory('test')
->select( array('name', 'surname') )
->where('id', '=', $user->id)
if ($active == 1) {
->where('status', '=', 1)
}
->order_by('docid', 'DESC')->find_all();

$query = ORM::factory('test')
->select( array('name', 'surname') )
->where('id', '=', $user->id);
if ($active == 1) {
query->where('status', '=', 1);
}
$query->order_by('docid', 'DESC')->find_all();

Related

Error in Laravel join

I am trying to get results from 2 table using join, where i want result between time range from those 2 tables,
here is my code
$final_trade = DB::table('finaltrade')
->join('exchanges', function($join)
{
$join->on('exchanges.id', '=', 'finaltrade.exchange_id')
->where('finaltrade.user_id', '=', Auth::user()->id)
->whereTime('finaltrade.created_at', '=', 'exchanges.start_time');
})
->get();
when i add hardcoded time value instead of 'exchanges.start_time' it gives correct result but i want to add values from column,
You have to tell Laravel that it isn't a value, but a column name:
->whereTime('finaltrade.created_at', '=', DB::raw('exchanges.start_time'));
The wrong code is ->whereTime('finaltrade.created_at', '=', 'exchanges.start_time'); change it to ->whereColumn('finaltrade.created_at', '=', 'exchanges.start_time');
And corrected code is
DB::table('finaltrade')
->join('exchanges', function($join) {
$join->on('exchanges.id', '=', 'finaltrade.exchange_id')
->where('finaltrade.user_id', '=', Auth::user()->id)
->whereColumn('finaltrade.created_at', '=', 'exchanges.start_time');
})->get();

mysql_num_rows in laravel?

im trying to use mysql_num_rows in laravel but laravel says it not the same way like in 'raw php'
example:
$users = DB::table('users')
->where('username', '=', $username)
->where('password', '=', $password)
->get();
what i want to do:
$count = mysql_num_rows($users);
if($count > 0 ){
$user->login = $request->login;
$user->email = $request->email;
$user->password = $request->password;
Auth::login($user);
return redirect("/");
}else{
return "datos incorrectos";
}
what laravel says:
Call to undefined function App\Http\Controllers\Auth\mysql_num_rows()
PD: its not philosophy of code just make commets about that question, i dont want answers like "u gonna crypt that thing?", "why not use [insert my faborite ORM]" is just a simple question THANKS
Instead of using mysql_* functions, you should use count() instead. It can be chained to Eloquent, query builder, or collections.
$users_count = DB::table('users')
->where('username', '=', $username)
->where('password', '=', $password)
->count();

Laravel 5 join table

Is any possible to join table users only on column where my user id != user id from db.
$games = Games::where('status', '=', 2)->where('owner_user_id', Auth::user()->id)->orWhere('join_user_id', Auth::user()->id)->orderBy('games.updated_at', 'desc')->paginate(10);
Question is:
How join user table, where owner_user_id and join_user_id != 30.
So I only want join user table for 1304 and 49. Its only example.
Games::where('status', '=', 2)
->join('users', function($join){
$join->on('users.id', '=', 'games.owner_user_id')
->where('user.id', '!=', Auth::user()->id);
})
->orderBy('games.updated_at', 'desc')
->paginate(10);
Something like this?
You need to use a use function to pass data to join:
$id = 1; // whatever id of other user
Games::where('status', '=', 2)
->join('users', function($join)use($id){ // Here you need to pass user Id
$join->on('users.id', '=', 'games.owner_user_id')
->where('user.id', '!=', $id);
})
->orderBy('games.updated_at', 'desc')
->paginate(10);

Select sql code in Laravel

Following query returning six values
SELECT tbl_start FROM timetable inner join route ON tbl_rte_id = id WHERE rte_origin = "UL" and rte_destination = "HW" ORDER BY(tbl_start) DESC;
And my laravel code is returning only one value
$tables = Timetable::join('route', 'tbl_rte_id', '=', 'id')
->where('rte_origin', $origin, 'AND')
->where('rte_destination', $destination)
->orderBy('tbl_start', 'desc')
->get();
foreach ($tables as $table) {
$result[$table->id] = $table->tbl_start;
}
This laravel code is not similar or similar. Can anyone help me.
Change this part:
->where('rte_origin', $origin, 'AND')
// to:
->where('rte_origin', $origin)
It will know by default that it's AND operator
And if you want to provide this operator, then do this:
->where('rte_origin', '=', $origin, 'AND')
You may try something like this:
$tables = Timetable::join('route', 'tbl_rte_id', '=', 'timetable.id')
->where('rte_origin', $origin)
->where('rte_destination', $destination)
->orderBy('tbl_start', 'desc')
->get()->lists('tbl_start', 'id');
The $tables will contain an array of id => tbl_start pairs.
Add a listener in your routes.php
Event::listen('illuminate.query', function($sql){
var_dump($sql);
});
Then execute both queries and check if you have the same result

Kohana 3.2 ORM - a sample of the unique values

Here is my query:
$comment = ORM::factory('comment')
->where('status', '=', 1)
->find_all();
field = user_id
How do I chose to only one of each comments in user_id?
The following:
$comment = ORM::factory('comment')
->distinct('user_id')
->where('status', '=', 1)
->find_all();
Displays everything in the database!
You code should be more like this..
$comment = ORM::factory('comment')
->where('status', '=', 1)
->group_by('user_id')
->find_all();
Check the documentation for more info.