class Model_User extends Orm\Model { static $_has_many = array( 'projects' ); } class Model_Project extends Orm\Model { static $_belongs_to = array( 'user' ); }
$projects = Model_User::find($user_id)->projects;
$user = Model_User::find()->where('id', $user_id) ; if($user) { $projects = $user->related('projects')->get_one(); if($projects) { // return the project details } else { // return false } }
$user = Model_User::find()->where('id', $user_id)->related('projects')->get_one(); if ( ! empty($user)) { return $user->projects; // can also be an empty array } else { return false; // $user_id was not found }Now if you're completely, 100% sure, no user could ever change the URL or $_POST values for another $user_id input. Then you don't need this, in all other cases you do. In your code the $user object will be an Orm\Query object and thus always be true. Which is why you need to retrieve the object first before you check if it exists. If it doesn't exist I made it return false, otherwise it returns the projects array (which could possibly still be empty, but is valid).
$user = Auth::instance()->get_user_id(); $this->user = Model_User::find()->where('id', $user[1]) ;
if ( ! empty($this->user)) { return $this->user->related('projects')->get_one(); } else { return false; // $user_id was not found }
It looks like you're new here. If you want to get involved, click one of these buttons!