FAQ Database Discussion Community


Phalcon | One query insted of two for “belongsTo”

php,mysql,join,phalcon,belongs-to
Is it possible to make one JOIN-query instead of two SELECT-queries if a model has relation belongsTo? I mean that if there's two models: <?php class Robots extends \Phalcon\Mvc\Model { public $id; public $name; public $type_id; public function initialize() { $this->belongsTo("type_id", "RobotsTypes", "id"); } } and <?php class RobotsTypes extends...

Relate Comments with themselves

mysql,cakephp,has-many,belongs-to
I'm new to cakePHP and the whole MVC architecture. I want to build a Reddit-like comment system where one Comment hasMany Comment(s) but also one Comment belongsTo (only) one Comment. It is the usual comment section where one comment can has a reply to that comment and the reply can...

Ruby on Rails: Child Class Gives Parent Class values when child class is created

ruby-on-rails,ruby,parent-child,belongs-to
I am basically looking for the opposite of this: Ruby on Rails: Building a child with default values when its parent is created But I have not been able to find anything on stack overflow or the documentation to assist me with this. class GeneralError < Details #ASSOCIATIONS belongs_to :type...

Laravel belongsTo throwing undefined function App/BelongsTo() exception

php,sql,laravel,eloquent,belongs-to
I have a comments table and user table with the relationship: user->hasMany('comments') and comment->belongsTo('user'). For some reason with which eludes me, I keep getting this FatalErrorException in Comment.php line 22: Call to undefined function App\belongsTo() My other models have no issue whatsoever with the HasMany relations, however, the comments model...

Issue with belongs_to and user (devise)

ruby-on-rails,devise,user,belongs-to
I'm new to RoR, and I would like to develop an app, but I have an issue with the belongs_to association. I am using devise for the authentication of my users, and I have an object called timesheet, I followed several tutorials and read a lot of forums but unfortunately...

Using this.store.filter with the ID of the belongs_to model

ember.js,filter,belongs-to
I'm trying to retrieve all the layouts for a given account. /app/models/account.js import DS from 'ember-data'; export default DS.Model.extend({ companyName: DS.attr('string'), layouts: DS.hasMany('layout') }); /app/models/layout.js import DS from 'ember-data'; export default DS.Model.extend({ name: DS.attr('string'), account: DS.belongsTo('account', { async: true }) }); /app/routes/layouts.js import Ember from 'ember'; export default Ember.Route.extend({ model:...

one model belongs to two models

ruby-on-rails,ruby,activerecord,belongs-to
I know similar questions have been asked before, but can't find one that's close enough to what I need for me to learn how to do this. I have 3 models: wedding, reception, and attendee The idea is that a user will be able to go to the wedding's event...

Creating relationship between 2 tables in CakePHP using models to write less code

php,cakephp,model,has-many,belongs-to
I try to say just CkMmUsersKta.user_id=CkSetupUser.id and define relationship between 2 tables in CakePhp. I defined models and called models in function in controller but says Undefined index for $c['CkSetupUser']['user_name'] CakePHP's Notice (8): Undefined index: CkSetupUser [APP\Controller\RealsController.php, line 67] my controller: class RealsController extends AppController { public $helpers = array('Html',...

CakePHP3 - Parent BelongsTo - Database Error PDOException

php,pdo,parent,belongs-to,cakephp-3.0
What am i doing wrong here, or is this some kind of Bug? On TicketsTable.php /** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { $this->table('tickets'); $this->displayField('title'); $this->primaryKey('id'); $this->addBehavior('Timestamp', [ 'events' => [ 'Tickets.closed' => [ 'close_date'...

Can't figure out has_many/belongs_to RAILS 4

ruby-on-rails,ruby-on-rails-4,database-migration,has-many,belongs-to
My models are: class CarBrand < ActiveRecord::Base has_many :car_models end class CarModel < ActiveRecord::Base belongs_to :car_brand end and my migrations are class CreateCarBrands < ActiveRecord::Migration def up create_table :car_brands do |t| t.string "brand", :limit => 20 t.timestamps null: false end end def down drop_table :car_brands end end class CreateCarModels <...

laravel two identical belongsTo() relations, one returns an array another string

mysql,database,laravel,relational-database,belongs-to
I don't get it I have two foreign keys identically set and two belongsTo() functions but one of them doesn't work right, instead of returning an array it just returns a string value from database. Here are the tables that I have in database. Table pages ----------------------------------------------------- | id |...

Rails JSON Rest API: Getting Child Class from Parent via API Call

ruby-on-rails,json,rest,has-many,belongs-to
I have two API controllers in my Rails app for a RESTful setup: StoresController (has many products) ProductsController (has one store) How can I write the API so that http://localhost:3000/api/v1/stores/37/products returns only the products for that store(in this case store #37)? I think I'm missing a route and controller method...

belongs_to with foreign_key not working in one direction

ruby-on-rails,foreign-keys,belongs-to
class Position < ActiveRecord::Base belongs_to :product, foreign_key: :symbol end class Product < ActiveRecord::Base has_many :positions, primary_key: :symbol, foreign_key: :symbol end When I do Product.first.positions.first I am getting a Product back. But, when I do Position.first.product I am getting nothing. When I look at the SQL generated by the query, it...

Rails 4 Override belongs_to setter

ruby-on-rails,override,setter,belongs-to
I am having an issue overriding a setter on a belongs_to attribute. I have the following: class Task < ActiveRecord::Base belongs_to :parent_task, :class_name => 'Task', :foreign_key => 'parent_task_id' def parent_task=(value) write_attribute(:parent_task, value) unless value == nil #remove all groups_belonging_to if this has been made into a child task -i.e. if...

Input new record into database using a form with the association belongs_to

ruby-on-rails,controller,associations,belongs-to,button-to
I’m relatively new to ruby on rails so bear with me. I'm trying to create an app where there are many Projects with specific Issues associated to each project. The following is my code: My Models: class Issue < ActiveRecord::Base belongs_to :project end class Project < ActiveRecord::Base has_many :issues has_and_belongs_to_many...

Rails select_tag will not access belongs_to relation

ruby-on-rails,ruby,model,html-select,belongs-to
I am trying to do a select_tag "employee_compensation_benefits_selection", options_from_collection_for_select(@employees, "id", "entity.name", "1") but entity.name wont work throws an undefined method `entity.name'. "entity" belongs to another model. through an entity_id class Employee < ActiveRecord::Base include UUIDHelper belongs_to :entity has_one :status has_many :restdays has_one :regular_work_period validates_presence_of :entity end require 'file_size_validator' class Entity...

Laravel / Eloquent - custom relation method

laravel,eloquent,relation,belongs-to
I have a class Report which has a belongsToMany relation to Metric. Report also additionally has a belongsTo relation to Metric. Normally, the model returned by the belongsTo relation is the same as one of the models in the belongsToMany relation. When this is true I'd like it to be...

Grails belongsTo multiple classes

grails,gorm,belongs-to
I have a domain class that can belong to one of several classes. I'm seeing validation errors when I try to save. class Teacher { Book book } class Student { Book book } // book can belong to either a student or a teacher class Book { static belongsTo...