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 8262341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:43:40+00:00 2026-06-08T03:43:40+00:00

I’m using Kohana for some time now. I’m using has_many, has_one, belongs_to without a

  • 0

I’m using Kohana for some time now. I’m using “has_many”, “has_one”, “belongs_to” without a problem, my question is:

If I have 2 tables like this:

tbl_foo1

id | tbl_foo2_id | field1
-------------------------
1  | 2           | bar
2  | 1           | foo

tbl_foo2

id | field1
-----------
1  | foo
2  | bar

I have to have a relation like: tbl_foo1 belongs to tbl_foo2 and tbl_foo2 has many tbl_foo1
So far so good.

The problem is when I try to save the relation in the controller..
So I have this code:

$t1 = ORM::factory('tbl_foo1')->values($values, $expected)->create();
$t2 = ORM::factory('tbl_foo2', $_POST['id']);
$t1->tbl_foo2_id = $t2;
$t1->save();

Ok, this should work, but I think this is not the best solution neither the most beautiful. For 2 reasons, 1 because of performance – it does 3 queries (1 to save $t1, 2 to find $t2, 3 to save relations of $t2 with $t1) and 2 because it may save empty records, because it doesn’t validate the existence of the records in $t2.

So my main questions is, how this should be done?


My own solution

I used the solution that biakaveron gave me, thank you.
But still, there was the validation problem.. so I spent some time and came up with a solution, using the same example:

$t2 = ORM::factory('tbl_foo2', $_POST['id']);
$t1 = ORM::factory('tbl_foo1')->values($values, $expected);
$t1->foo2 = $t2; // foo2 is a belongs_to relationship
$t1->create();

in the tbl_foo1 model I added the rules function:

public function rules()
{
    'foo2_id' => array('not_empty')
}

So this way, I don’t have to do another query, which is good for performance and it’s a simple way to do it. And it works, because, when the ORM tries to find the id of the model tbl_foo2, if it doesn’t find it, it returns NULL, so it will always empty no matter what!

Note: I used the DB transactions too (because this is just a portion of the process), so if one of the queries breaks for some reason, well no query will run. Just remember, you have to use one database engine that supports transactions (mine is InnoDB).

  • 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-08T03:43:41+00:00Added an answer on June 8, 2026 at 3:43 am

    1 You can save model with belongs_to object:

    $t2 = ORM::factory('tbl_foo2', $this->request->post('id'));
    if (!$t2->loaded()) 
    {
        // wrong ID for tbl_foo2
    }
    
    $t1 = ORM::factory('tbl_foo1')->values($values, $expected);
    $t1->foo2 = $t2; // foo2 is a belongs_to relationship
    $t1->create();
    

    2 Check tbl_foo2_id with special callback.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.