Menu
  • HOME
  • TAGS

how to define a relationship in Yii2 with orOnCondition

yii2,database-relations

Found the answer, don't use hasMany, instead just return an ActiveQuery: <?php class Job extends ActiveRecord { public function getAuditFields() { return AuditField::find() ->orOnCondition([ 'audit_field.model_id' => $this->job_id, 'audit_field.model_name' => get_class($this), ]) ->orOnCondition([ 'audit_field.model_id' => ArrayHelper::map($this->getJobTeches()->all(), 'id', 'id'), 'audit_field.model_name' => 'app\models\JobTech', ]); } } ...

rails ordering by has many relation

ruby-on-rails-4,order,has-many,database-relations

Not the best, but the simplest solution is @concerts = Concert.all.includes(:reviews).sort_by{|concert| concert.reviews.size}.reverse ...

MySQL require multiple values on same column

mysql,join,distinct,having,database-relations

As Alexandre in his comment said, your design is more than problematic. Your example is faulty too: T The categories 8 - 12 should have the gid 2 not 1. The query should return "Henry Ford" too, because he lives in the USA and works on Cars. Following not beautiful...

MVC dropdownlist with values ​​from the database

asp.net-mvc,entity-framework,database-relations

Assuming that you mean to have the second drop-down's values updated each time a different value is selected in the first drop-down, one way to do it is via jQuery, JSON, a view model and some controller actions (note this doesn't implement best practices like not having database calls in...

Laravel - Database relationships

laravel,model,database-relations

There are three different approaches, in your question you're mixing them up a little. I'll go through all of them. Many-to-many A many-to-many relationship would mean, in your example, that a company can have multiple employees and that an employee can work for multiple companies. So when you're using the...

Database organization for separating two different types of the same model

ruby-on-rails,relationships,database-relations

You can use single table inheritance class Skill < ActiveRecord::Base end class WantedSkill < Skill belongs_to :user end class PossessesSkill < Skill belongs_to :user end Your skills table should have a column called type where the type of the skill will be stored. WantedSkill.create(:name => "html") Above will save the...

Laravel and relationships

php,laravel,database-relations

You have tables as given below: Table: NewsTitles: (id, name) Table: News: (id, news_titles_id, create_time) Models for that: class NewsTitles extends Eloquent { protected $table = 'NewsTitles'; public function news() { return $this->hasOne('News', 'news_title_id'); } } class News extends Eloquent { protected $table = 'News'; public function title() { return...

Should I change/normalize my ER (1:1) diagram?

database,database-design,entity-relationship,database-relations,er-diagrams

The relationship expressed in your ER diagram appears to be one-to-many, not one-to-one (because DocumentId alone is not unique in the Version table.) Normalization should always be based on an understanding of the business rules that are applicable in your intended design. It isn't sufficient to draw conclusions based only...

Laravel 5 - Elequent GROUP BY is failing

php,mysql,laravel-5,database-relations

If I understand correctly, you'd like to fetch a list of Content objects together with their children Content objects, correct? Easiest way to do that is to create a parent-child relation in your Eloquent Content model and then use that to load parents with children: <?php class Content extends Model...

Yii2. “With” in ManyToMany

php,yii2,database-relations

You'll have to fetch the images first and then provide an extra getter function to return the files: public function getImageFiles() { $files = []; foreach ($this->images as $image) $files[] = $image->file; return $files; } ...