Welcome, Guest
Username: Password: Remember me

TOPIC: [ADDED] Exploded prepareQuery() - ORM reduced

[ADDED] Exploded prepareQuery() - ORM reduced 05 Jan 2017 14:18 #14913

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Will there be a way to have the builder generate the original [non-ORM] prepareQuery with its filters?
	/**
	* Preparation of the list query.
	*
	* @access	protected
	* @param	object	&$query	returns a filled query object.
	*
	* @return	void
	*/
	protected function prepareQuery(&$query)
	{

		$acl = TestHelper::getActions();

		//FROM : Main table
		$query->from('#__users AS a');



		//IMPORTANT REQUIRED FIELDS
		$this->addSelect(	'a.id');


		switch($this->getState('context', 'all'))
		{

			case 'all':
				//SELECT : raw complete query without joins
				$this->addSelect('a.*');

				// Disable the pagination
				$this->setState('list.limit', null);
				$this->setState('list.start', null);
				break;
		}

		//FILTER - Access for : Root table


		//WHERE - FILTER : created_by
		if($filter_created_by = $this->getState('filter.created_by'))
		{
			if ($filter_created_by == 'auto'){
				$this->addWhere('a.created_by = ' . (int)JFactory::getUser()->get('id'));
			}
			else 
			if ($filter_created_by > 0){
				$this->addWhere("a.created_by = " . (int)$filter_created_by);
			}
		}


		//WHERE - FILTER : modified_by
		if($filter_modified_by = $this->getState('filter.modified_by'))
		{
			if ($filter_modified_by == 'auto'){
				$this->addWhere('a.modified_by = ' . (int)JFactory::getUser()->get('id'));
			}
			else 
			if ($filter_modified_by > 0){
				$this->addWhere("a.modified_by = " . (int)$filter_modified_by);
			}
		}


		//WHERE - FILTER : user_id
		if($filter_user_id_user_id = $this->getState('filter.user_id_user_id'))
		{
			$this->addJoin("`#__test_users` AS _user_id_ ON _user_id_.id = a.user_id", 'LEFT');
			if ($filter_user_id_user_id == 'auto'){
				$this->addWhere('_user_id_.user_id = ' . (int)JFactory::getUser()->get('id'));
			}
			else 
			if ($filter_user_id_user_id > 0){
				$this->addWhere("_user_id_.user_id = " . (int)$filter_user_id_user_id);
			}
		}


		//WHERE - FILTER : user_id
		if($filter_user_id = $this->getState('filter.user_id'))
		{
			if ($filter_user_id == 'auto'){
				$this->addWhere('a.user_id = ' . (int)JFactory::getUser()->get('id'));
			}
			else 
			if ($filter_user_id > 0){
				$this->addWhere("a.user_id = " . (int)$filter_user_id);
			}
		}


		// Apply all SQL directives to the query
		$this->applySqlStates($query);
	}
The administrator has disabled public write access.

[ADDED] Exploded prepareQuery() - ORM reduced 05 Jan 2017 14:43 #14914

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
For simple filters, yes, but for complex such as lists, accesses (+propagation), pivots... it will still use the ORM because it has always been such.

If you look at your previous components the ORM is there already for those special ones. (The functions are still there at the moment)
I wrote ORM for simplificating the fields and for other coming features.
I will not remove this layer even with an option. Simply exploding the construction with non-ORM statements, and only for the simple ones.
Believe me, I cannot put all the model behaviors repeated in every prepareQuery() function. It would be insane.

ORM is not a complex framework it is simply a very light constructor for avoiding repetition of code, nothing more.
I don't want to do the revolution with :
$city = new City();
This would be a complex useless layer for us. Not needed.

So the option I will propose to you is prepareQuery() exploded. So it will be more easy for you to see the SQL query construction.

BTW, when you want to dump you SQL query, use the queryDump() function. (ORM class)
It is quite usefull for working with your queries.
Coding is now a piece of cake
The administrator has disabled public write access.

[ADDED] Exploded prepareQuery() - ORM reduced 09 Jan 2017 13:01 #14927

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
This monday, you can enjoy the ORM option for building.

In the component configuration, you will find "SQL Queries" parameter.
Choose "Exploded", and tell me if this fits to your needs.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos

[ADDED] Exploded prepareQuery() - ORM reduced 09 Jan 2017 16:30 #14929

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Thanks so very much Jocelyn, I believe this offers me the best of both worlds. I can play and get comfortable with ORM and still meet the delivery/maintenance schedules of current "legacy" projects; being able to benefit from v3.1 updates and modifications is a plus. :woohoo:

I hope I am not alone and that it will help others who maybe in a similar predicament.

Warm regards
v
The administrator has disabled public write access.

[ADDED] Exploded prepareQuery() - ORM reduced 10 Jan 2017 17:02 #14932

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 278
  • Thank you received: 35
  • Karma: 22
This is an excellent feature! Great thing to mix ORM and custom queries ...

So, in a case 'layout.xxxx' context, that mix ORM and SQL query, how to use the queryDump() function ?
the code below just give the FROM query and nothing else :
                $this->orm->queryDump($query);

                // Apply all SQL directives to the query
                $this->applySqlStates($query);
stdClass Object
(
    [FROM] => #__jbreeding_pedigrees AS a
)

Please, I need the right syntax to debug the full query ...
Last Edit: 10 Jan 2017 17:04 by liubov.
The administrator has disabled public write access.

[ADDED] Exploded prepareQuery() - ORM reduced 10 Jan 2017 19:23 #14934

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
AFTER
$this->applySqlStates($query);
Coding is now a piece of cake
The administrator has disabled public write access.

[ADDED] Exploded prepareQuery() - ORM reduced 10 Jan 2017 22:31 #14935

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 278
  • Thank you received: 35
  • Karma: 22
Whaoo ! Excellent !!!
The administrator has disabled public write access.
Time to create page: 0.102 seconds

Get Started