Skip to content

Querying Sql Model relationship existence with mongo model #1035

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

Closed
haridarshan opened this issue Nov 16, 2016 · 2 comments · Fixed by #1195
Closed

Querying Sql Model relationship existence with mongo model #1035

haridarshan opened this issue Nov 16, 2016 · 2 comments · Fixed by #1195

Comments

@haridarshan
Copy link

haridarshan commented Nov 16, 2016

Hi,

Getting this error when am querying sql model with mongo model relation existence

Fatal error:  Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exists ?) order by `modified_at` desc' at line 1 in /var/www/test/vendor/illuminate/database/Connection.php:333
Stack trace:
#0 /var/www/test/vendor/illuminate/database/Connection.php(333): PDO->prepare('select * from `...')
#1 /var/www/test/vendor/illuminate/database/Connection.php(722): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\MySqlConnection), 'select * from `...', Array)
#2 /var/www/test/vendor/illuminate/database/Connection.php(685): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#3 /var/www/test/vendor/illuminate/database/Connection.php(349): Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#4 /var/www/test/vendor/illuminate/database/Query/Builder.php(161 in
/var/www/test/vendor/illuminate/database/Connection.php on line 729 

Sql Model

namespace App\Models\Sql;

use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\HybridRelations;

class History extends Model
{
	use HybridRelations;
	
	/** @var string Connection Name */
	protected $connection = 'default';
	
	/**
	 * History with Campagin Details
	 * @return \Jenssegers\Mongodb\Relations\HasOne
	 */
	public function campaign()
	{
		return $this->hasOne('\App\Models\Mongo\Campaign', 'campaign.campaign_id', 'campaign_id');
	}
}

This way am checking relation existence. If i remove has it works fine but using has throws the PDOException error

$history = History::where([
	// some condition
])
->has('campaign')
->with('campaign')
->orderBy('modified_at', 'desc')
->get();

and here is the sql query formed

select * from `user_campaign_history` where ( <some_conditions> = ?) and exists (select * from `campaigns` where `campaign`.`campaign_id` exists ?) order by `modified_at` desc
@michaeljdufresne
Copy link

+1 Same issue, has() or whereHas() give the same results.

@vesper8
Copy link

vesper8 commented Mar 21, 2017

having same issue and pulling my hair out! How to use has() with hybrid relations when parent is mongo and children are mysql?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants