model-associations

CakePHP 3.0 Can't save hasMany associated data

此生再无相见时 提交于 2019-12-10 14:58:51
问题 I've problem when saving data with hasMany association This is my table 1) post table: each item has an unique id. id | title | ... 1 | Aloha | ... 2) images table id | post_id | image | ... 1 | 1 | abc.jpg | ... 2 | 1 | efg.jpg | ... My Model (Table) Posts Model // PostsTable.php <?php namespace App\Model\Table; use Cake\ORM\Query; use Cake\ORM\Table; use Cake\Validation\Validator; class PostsTable extends Table { public function initialize(array $config) { $this->table('posts'); $this-

Data Modeling 3 Way Table has_many association

匆匆过客 提交于 2019-12-10 14:05:42
问题 I am attempting to build a table to handle both the location and category a certain campaign has been set to with the following model associations: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy has_many :metros, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Metro < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids

has_one nested attributes not saving

自闭症网瘾萝莉.ら 提交于 2019-12-09 18:59:01
问题 I have two models Project and ProjectPipeline. I want to create a Project form that also has fields from the ProjectPipeline model. I have created the form successfully but when I hit save the values aren't stored on the database. project.rb class Project < ActiveRecord::Base has_one :project_pipeline accepts_nested_attributes_for :project_pipeline self.primary_key = :project_id end projectpipeline.rb class ProjectPipeline < ActiveRecord::Base belongs_to :project, autosave: :true validates

Rails 4: Difference between validates presence on id or association

孤者浪人 提交于 2019-12-09 02:49:07
问题 If I have a 'belongs_to' association in a model, I'd like to know the notional difference between validating an association: class Topping < ActiveRecord::Base belongs_to :pancake validates :pancake, presence: true ... and validating the associated model id: class Topping < ActiveRecord::Base belongs_to :pancake validates :pancake_id, presence: true ... Motivation: Some code which assigned a topping to a pancake stopped working at some time in the past. Changing the validation from

filter conditions from an association

眉间皱痕 提交于 2019-12-08 21:42:30
I have a search function, which works great for staff, so I can search by name. Now, I want filter staffs by staffgroup.groupname, but unfortunatelly i get this error: Column not found: 1054 Unknown column 'staffgroups.groupname' in 'where clause' I'm having the following tablelayout staffs (a person can belong to many groups) staff_staffgroups (HABTM-linking table) staffgroups (has a groupname) i used the conditions as follows: $tmpConditions['AND'][] = array('Staff.isActive =' => "1"); $tmpConditions['OR'][] = array('Staff.lastname LIKE' => "%$name%"); $tmpConditions['OR'][] = array(

filter conditions from an association

若如初见. 提交于 2019-12-08 04:37:43
问题 I have a search function, which works great for staff, so I can search by name. Now, I want filter staffs by staffgroup.groupname, but unfortunatelly i get this error: Column not found: 1054 Unknown column 'staffgroups.groupname' in 'where clause' I'm having the following tablelayout staffs (a person can belong to many groups) staff_staffgroups (HABTM-linking table) staffgroups (has a groupname) i used the conditions as follows: $tmpConditions['AND'][] = array('Staff.isActive =' => "1");

Refactoring has_many with scopes

天涯浪子 提交于 2019-12-08 03:06:36
问题 I'm a newbie and I just showed my code to an expert, that told me I shouldn't use has_many to filter my variables, but scopes . I have three models : User, Product and Ownership. So here is my code in app/models/user.rb : class User has_many :ownerships, foreign_key: "offerer_id", dependent: :destroy has_many :owned_products, through: :ownerships, source: :product has_many :future_ownerships, -> { where owning_date: nil, giving_date: nil }, class_name: "Ownership", foreign_key: "offerer_id"

Source Reflection Errors with has_many :through

爷,独闯天下 提交于 2019-12-07 09:23:07
问题 I'm attempting to create a system where my site's users can favorites pages. Those pages have two types, either clubs or sports. So, I have four models, associated as such: User Model: class User < ActiveRecord::Base .. has_many :favorites has_many :sports, :through => :favorites has_many :clubs, :through => :favorites .. end Favorites Model: class Favorite < ActiveRecord::Base .. belongs_to :user belongs_to :favoritable, :polymorphic => true end Club Model: class Club < ActiveRecord::Base ..

Save data in a cakephp hasmany and belongsto association at once

倾然丶 夕夏残阳落幕 提交于 2019-12-06 08:13:15
问题 I know this question is asked here a many times but I also tried to follow the solutions provided at my best. As I am learning a cakephp some solutions seemed difficult to implement in a code. I am using cakephp 2.5. What I am trying to do is creating a problem report with attached one or more uploads. Here is some of what I have implemented so far:- I have following models: Candidate CandidatesProblemReport CandidatesProblemReportsUpload There associations are as follows:

belongs_to belongs_to association only no has_many or has_one

假如想象 提交于 2019-12-06 06:54:59
Can you have a belongs_to belongs_to relationship in Rails? Search results gave me two results. The fact that I can't find very much info on the subject, seems to indicate that it shouldn't be done or is bad practice. I asked yesterday about a has_many relationship, but thought because I couldn't find information on this, I would generate a question so it is easier for people to search for this in the future. I'm paraphrasing another users answer (I hope this is ok). A Shoe can have many Socks, but only one active Sock. Other Socks are inactive. All Socks are also unique with unique patterns.