nystudio107 / craft-connect

Allows you to connect to external databases and perform db queries
MIT License
16 stars 5 forks source link

Working example of using createCommand with craft-connect? #3

Closed Geestig closed 4 years ago

Geestig commented 5 years ago

Hi. First, thanks for this great plugin! However I seem to be unable to actually use the createCommand function. I'm trying to do a simple INSERT INTO but can't get it to work.

The error i'm receiving is the following: "Call to a member function getQueryBuilder() on string"

{% set db = craft.connect.open('remote') %}
{% set sql = 'INSERT INTO tblContact_Management (city, email, lang, lastname) VALUES (citytest, mailtest, nl, De La Phantoma)' %}
{% set projectResults = craft.connect.query(db)
  .createCommand(sql)
%}

Here's my actual code for trying to insert something. I have no issues on retrieving data though.

khalwat commented 4 years ago

So Connect returns a Query object from .query(): https://github.com/nystudio107/craft-connect/blob/v1/src/variables/ConnectVariable.php#L63

But it returns a Connection object from .open(): https://github.com/nystudio107/craft-connect/blob/v1/src/variables/ConnectVariable.php#L39

So you can do {% do db.createCommand(sql) %}

See also: https://nystudio107.com/blog/cutting-the-cord-removing-plugins#plugin-4-connect

Geestig commented 4 years ago

Worked like a charm, thanks for the reply!