Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:28:35+00:00 2026-06-18T11:28:35+00:00

I use PHPActiveRecord. Assume I’ve a table Manager and a table Employees. A Manager

  • 0

I use PHPActiveRecord. Assume I’ve a table Manager and a table Employees. A Manager has multiple Employees and an Employee has one Manager.

I’ve defined this association:

  • model Manager: static $has_many = { array('employee')}
  • model Employee: static $belongs_to = { array('manager')}

$manager->employee gives me an array with Employee instances.

How do I select the oldest Employee? Or the name of the last Employee. How can I use conditions in a associative data?
I can extend the association like below:

  • model Manager: static $has_many = { array('employee', 'order' => 'age desc', 'limit => 1')}

or

  • model Manager: static $has_many = { array('employee', 'order' => 'id desc', 'limit => 1')}

But with this solution I’ve to create a new association for every new case.

Does a better solution exists? Something like below?

  • $manager->employee->last()->name to select the name of the last employee

  • $manager->employee->find('limit => 1, 'order' => 'age desc') to select the last employee

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:28:37+00:00Added an answer on June 18, 2026 at 11:28 am

    This is a bit old of a question, but I feel it at least requires an answer.

    You can define multiple relations that you want for the same class. I’m not sure if you explicitly want to avoid that, and sans modifying the framework to have $manager->employee-> return some sort of object you’re out of luck.

    public static $has_one = array(
        array('last_employee', 'class_name'=>'Employee',/** conditions **/ 'foreign_key'=>'manager_id'),
        array('oldest_employee', 'class_name'=>'Employee',/** conditions **/ 'foreign_key'=>'manager_id'),
    );
    

    You can then call $manager->last_employee; or $manager->oldest_employee.
    I feel like even something like scopes doesn’t exactly give what you want, so this seems like a good solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use PHP ActiveRecord in Codeigniter. I have a model like this:
I cannot access joined table with PHPActiveRecord/Twig. Here is simplified code. It has two
use Text::Table; my $tb = Text::Table->new(Planet,Radius\nkm,Density\ng/cm^3); $tb->load( [ Mercury,2360,3.7], [ Mercury,2360,3.7], [ Mercury,2360,3.7], );
Use case: A user creates a new task, which has to be sent upstream
Use-case: A Xib is defined with a single simple UIView A custom view class
use strict; use Time::HiRes qw[gettimeofday tv_interval]; my $start_index = int(rand(50))+100;#this value is arbitrary for
Use of Unassigned local variable 'model'. Is what error message I'm getting. Its right
Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt
Use case : one Java process with one or two C++ processes, always on
use DBI(); What is causing this insert to fail to err 1, the SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.