New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] Multiple Modules Models Alias and leftJoin #1111

Closed
ovr opened this Issue Aug 19, 2013 · 11 comments

Comments

Projects
None yet
4 participants
@ovr
Contributor

ovr commented Aug 19, 2013

Hi all,
I have model

class Event
  extends Model
{
  public $id;
  public $created_at;
  public $user_id;
  public $type_id;
  public $product_id;

  public function initialize()
  {
    $this->hasOne('product_id', 'Product\\Model\Product', 'id', array(
       'foreignKey' => true,
       'alias' => 'Product'
    ));
    $this->hasOne('user_id', 'User\\Model\User', 'id', array(
      'foreignKey' => true,
      'alias' => 'User'
    ));
    $this->hasOne('type_id', 'User\\Model\Events\Type', 'id', array(
      'foreignKey' => true,
      'alias' => 'Type'
    ));
    $this->hasMany('user_id', 'User\\Model\Follow\User', 'author_user_id', array(
      'foreignKey' => true,
      'alias' => 'Followers'
    ));
  }

  /**
   * Independent Column Mapping.
   */
  public function columnMap() {
    return array(
      'id' => 'id',
      'created_at' => 'created_at',
      'user_id' => 'user_id',
      'type_id' => 'type_id',
      'product_id' => 'product_id'
    );
  }
}

I would like to use

        $builder = $this->modelsManager->createBuilder('e')
            ->addFrom('User\Model\Event', 'e')

            ->leftJoin('e.User', null, 'u')

On Forum http://forum.phalconphp.com/discussion/766/multiple-relations-and-left-joind-models

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@jodator

This comment has been minimized.

jodator commented Aug 19, 2013

You can use multiple joins from the same model just like you wrote.

$builder->join('Tagged', 'Category.type = "Category" and Category.foreignKey = User.id', 'Category');
$builder->join('Tagged', 'Group.type = "Group" and Group.foreignKey = User.id', 'Group');

If this is not the case of your question please describe more clearly ;)

@ovr

This comment has been minimized.

Contributor

ovr commented Aug 19, 2013

@jodator
I have multi modules models structure and i want to use alias what i inizialize in model to query and don`t write like it Category" and Category.foreignKey = User.id
but when i try to write in 1 arg leftJoin Product
it throws exception like \Product Model not found
sry for my english

@jodator

This comment has been minimized.

jodator commented Aug 19, 2013

Ah, OK. I didn't use multi modules structure before. But if Phalcon is not using aliases then it would be nice if it does use them.

@ovr

This comment has been minimized.

Contributor

ovr commented Aug 19, 2013

@jodator Now i am porting big application more then 30k lines from zf1+doctrine1 to phalcon and now have big problem with no leftjoin on alias and 100500 select query on primary key when i use relations.
exml

$products = \Product\Model\Products\Table::find()**; // 1 select for 10 products
foreach($products as $product)
{
    echo $product->User->username; // 1 select for alias User 
}

Total 1*10 + 1 =11selects
It is very sad that so bad right now

@ghost

This comment has been minimized.

ghost commented Aug 19, 2013

I think you can use something like this:

<?php

namespace Model\Extreme;

class ProfileNote extends \Phalcon\Mvc\Model
{
    public $id;
    public $user_id;
    public $notes;
    public $added_by;
    public $dt;
    public $edited_by;
    public $edited_dt;

    /**
     * @return string
     * @global $wpdb
     */
    public function getSource()
    {
        global $wpdb;
        return $wpdb->prefix . 'em_profile_notes';
    }

    public function initialize()
    {
        $this->belongsTo('user_id',   '\\Model\\WordPress\\User', 'ID', array('alias' => 'User'));
        $this->belongsTo('added_by',  '\\Model\\WordPress\\User', 'ID', array('alias' => 'Creator'));
        $this->belongsTo('edited_by', '\\Model\\WordPress\\User', 'ID', array('alias' => 'Editor'));
    }

    public static function getComments($user_id)
    {
        $di   = \Phalcon\DI::getDefault();
        $mgr  = $di->get('modelsManager');
        $phql = "
            SELECT Note.*, Creator.*, Editor.*
                FROM \\Model\\Extreme\\ProfileNote AS Note
                    LEFT JOIN \\Model\\WordPress\\User AS Creator ON added_by = Creator.ID
                    LEFT JOIN \\Model\\WordPress\\User AS Editor ON edited_by = Editor.ID
                WHERE user_id = ?0
                ORDER BY dt
        ";

        return $mgr->executeQuery($phql, array(0 => $user_id));
    }
}
@ovr

This comment has been minimized.

Contributor

ovr commented Aug 19, 2013

@sjinks Cпасибо за подсказку но было бы тут одно такое место да. а когда тут 50 таблиц куча сущностей и еще большей связей нельзя так делать. а еще в моделях есть метод $this->getModelsManager() ))

@ghost

This comment has been minimized.

ghost commented Aug 20, 2013

А QueryBuilder заюзать не вариант?

@ovr

This comment has been minimized.

Contributor

ovr commented Aug 20, 2013

@sjinks Там в 1 аргументе нельзя использовать алиас он думает что это имя модели + надо дописывать связь и алиас в итоге это избыточно + он еще делает как я понимаю алиас токо для запроса а модель захочет использовать так $even->{"\User\Model\User"};

@ovr

This comment has been minimized.

Contributor

ovr commented Aug 24, 2013

Could anyone implement this?

@ovr

This comment has been minimized.

Contributor

ovr commented Nov 19, 2013

Now i am porting profiler from PhalconEye cms to current project on phalcon
On page 922 Mysql Db quieries Because phalcon doesn`t fetch result with leftJoin and when i use relation it go to select it from db on primary key
i use

            ->leftJoin('Product\Model\Product', 'e.product_id = Product.id', 'Product')
            ->leftJoin('User\Model\Shop', 'Product.shop_id = Shop.id', 'Shop')

But on foreach phalcon use select.
Its a realy hight task.

@phalcon It's really just a wild hole in ORM. While such things will not work excellent library can throw. People do not need a super fast framework that can`t perform 1 query and makes sense from hundreds of such a framework = zero.90% application work time is ORM not MVC and etc

ovr added a commit to ovr/cphalcon that referenced this issue Jan 15, 2014

@niden niden added Unassigned and removed Unassigned labels Apr 3, 2014

@stale

This comment has been minimized.

stale bot commented Apr 17, 2018

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

@stale stale bot added the stale label Apr 17, 2018

@sergeyklay sergeyklay closed this Apr 17, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment