I am using Propel as my DAL for my Symfony project. I can't seem to get my application to work across two or more databases.
Here's my schema.yml:
db1:
lkp_User:
pk_User: { type: integer, required: true, primaryKey: true, autoIncrement: true }
UserName: { type: varchar(45), required: true }
Password: longvarchar
_uniques:
Unique: [ UserName ]
db2:
tesco:
Id: { type: integer, required: true, primaryKey: true, autoIncrement: true }
Name: { type: varchar(45), required: true }
Description: longvarchar
And here's the databases.yml:
dev:
db1:
param:
classname: DebugPDO
test:
db1:
param:
classname: DebugPDO
all:
db1:
class: sfPropelDatabase
param:
classname: PropelPDO
dsn: 'mysql:dbname=bpodb;host=localhost' #where the db is located
username: root
password: #pass
encoding: utf8
persistent: true
pooling: true
db2:
class: sfPropelDatabase
param:
classname: PropelPDO
dsn: 'mysql:dbname=mystore2;host=localhost' #where the db is located
username: root
password: #pass
encoding: utf8
persistent: true
pooling: true
When I call php symfony propel-build-model, only db1 is generated, db2 is not.
Any idea how to fix this problem?
I got this issue working! The most important thing is you must name your schema according to %dbname%.schema.yml. In this way Symfony will be able to assign the ymls to the correct database.
Also when running the task you should specify the connection for example:
symfony propel:build-all-load --connection=my_connection
This worked for me, hope it helps.
You can also use Propel::getConnection('db2') to manually retrieve a connection.
Just have in mind that what you call "db1", "db2" are the connection names. You can have several connections to a same database with various login/permissions (like read only etc.).
It's very good for testing purpose: you can do it with the same connection name with a different database. No way to crash your production database with that :)
Related
I'm having this issue with a simple configuration when working with multiple datasources and I'm trying to save a new instance into a specific datasource
my datasources:
dataSources:
dataSource:
pooled: true
jmxExport: true
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
driverClassName: com.mysql.jdbc.Driver
username: root
password: Choice2016
dominio1:
pooled: true
jmxExport: true
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
driverClassName: com.mysql.jdbc.Driver
username: root
password: Choice2016
dominio2:
pooled: true
jmxExport: true
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
driverClassName: com.mysql.jdbc.Driver
username: root
password: Choice2016
my domain
class Nxtt_reports {
Boolean favorite
static hasMany = [nxtt_report_histories: Nxtt_report_history, nxtt_user_reports: Nxtt_user_reports, nxtt_report_snapshots: Nxtt_report_snapshot]
static constraints = {
}
static mapping = {
datasource 'ALL'
}
}
when I do this,
def nxtt = Nxtt_reports.class
println(nxtt.dominio1.list())
I can list data in the domain I want, but if I do this
def nxtt = Nxtt_reports.class.newInstance()
nxtt.favorite = 0
nxtt.dominio2.save()
I'm getting this
No such property: dominio2 for class: nexttreport.server.Nxtt_reports. Stacktrace follows:
groovy.lang.MissingPropertyException: No such property: dominio2 for class: nexttreport.server.Nxtt_reports
using:
| Grails Version: 3.1.1
| Groovy Version: 2.4.5
| JVM Version: 1.8.0_65
edit
environments:
development:
dataSources:
dataSource:
dbCreate: update
url: jdbc:mysql://192.168.1.24:3306/nexttreport?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
dominio1:
dbCreate: update
url: jdbc:mysql://192.168.1.24:3306/dominio1?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
dominio2:
dbCreate: update
url: jdbc:mysql://192.168.1.24:3306/dominio2?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
I have a bit of trouble reading a and understanding your code, because you are not following normal Java/Groovy conventions.
Firstly I would recommend using normal CamelCase and no underscores in your class names, so use NxxtReports.
Secondly, why not do NxxtReports.domino1.list() ?
list() is a static method on the NxxtReports, so call it like that.
Thirdly and I think most importantly, use the normal Java/Groovy way of instantiating an Object, so do
def nxxt = new NxxtReport()
nxxt.favorite = 0
nxxt.domino2.save()
I suspect that the extra Grails methods on a DomainClass instance are not correctly added by calling newInstance() on the class variable.
I don't have a real computer at hand (on mobile now) so I can't check my code, but I think using normal Java/Groovy conventions will help a lot.
I am a newbie to Grails and I am stuck with the GORM part. I tried to follow tutorial on this site http://grails.asia/grails-tutorial-for-beginners-introduction-to-gorm about GORM and the first thing to do is create a domain class. However, after following the instructions there, I always received the error showed below.
My controller for inserting record
package nba
class PlayerController {
def index() {
//render "It's game time! NBA Game time"
Player p = new Player()
p.fname = "Lebron"
p.lname = "James"
p.position = "Small Forward"
p.age = 30
}
}
. What is the problem with this. What am I lacking? Please help.
---
grails:
profile: web
codegen:
defaultPackage: myapp
info:
app:
name: '#info.app.name#'
version: '#info.app.version#'
grailsVersion: '#info.app.grailsVersion#'
spring:
groovy:
template:
check-template-location: false
---
grails:
mime:
disable:
accept:
header:
userAgents:
- Gecko
- WebKit
- Presto
- Trident
types:
all: '*/*'
atom: application/atom+xml
css: text/css
csv: text/csv
form: application/x-www-form-urlencoded
html:
- text/html
- application/xhtml+xml
js: text/javascript
json:
- application/json
- text/json
multipartForm: multipart/form-data
pdf: application/pdf
rss: application/rss+xml
text: text/plain
hal:
- application/hal+json
- application/hal+xml
xml:
- text/xml
- application/xml
urlmapping:
cache:
maxsize: 1000
controllers:
defaultScope: singleton
converters:
encoding: UTF-8
views:
default:
codec: html
gsp:
encoding: UTF-8
htmlcodec: xml
codecs:
expression: html
scriptlets: html
taglib: none
staticparts: none
---
hibernate:
cache:
queries: false
use_second_level_cache: true
use_query_cache: false
region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'
endpoints:
jmx:
unique-names: true
dataSource:
pooled: true
jmxExport: true
driverClassName: com.mysql.jdbc.Driver
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:mysql://localhost/grails_db
username: root
password:
test:
dataSource:
dbCreate: update
url: jdbc:mysql://localhost/grails_db
production:
dataSource:
dbCreate: update
url: jdbc:mysql://localhost/grails_db
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED
Sorry I didn't see your comment:
Firstly to expand on my comment above and explain grails controller behaviour better. I will stick with render you have other choices.
When declaring an action in your controller:
so lets say we have
class TestController {
def hello() { }
}
You may or may not have a domainClass bound to this example TestController, if you did the domain class would be {package}/Test.groovy {matching the naming construct of controller}.
In this example we have no domain controller and above action will show something as long as you create a folder under views called
test/hello.gsp
So in that test folder you create hello.gsp and put in there some html..
If you didn't have
test/hello.gsp
Then in your controller you would have to define what to render.
class TestController {
def hello() {
// render 'some text'
//render template: '/path/to/template', model: [params:params]
render view: '/path/to/gsp', model: [params:params]
}
}
You then state:
This is how I got the error, when I created a domain class player and
then put the field names (fname, lname, position, age), it was
successful, the table player is created in the database together with
field names but when I restarted running the server, the table was
deleted automatically and the running back the server returns error.
What is the problem with that?
Firstly you need to then ask yourself, did you configure/install some DB in order to see it working initially?
Grails provides you with an inbuilt DB that resides in your memory as part of its default app creation.In short a fresh DB is triggered on each run app.
E2A
Pay attention to dbCreate line:
create-drop : Creates tables on application startup and drops then when shutdown
create : Creates all tables at app start up
update : Updates existing tables (will not attempt to create any. that's down to you)
http://grails.github.io/grails-doc/3.0.x/guide/conf.html
In development mode dbCreate is by default set to "create-drop", but
at some point in development (and certainly once you go to production)
you'll need to stop dropping and re-creating the database every time
you start up your server.
I needed to send an email through symfony but in my factories.yml it has something like this
all:
routing:
class: sfPatternRouting
param:
generate_shortest_url: true
extra_parameters_as_query_string: true
view_cache_manager:
class: sfViewCacheManager
param:
cache_key_use_vary_headers: true
cache_key_use_host_name: true
And I modify them to something like:
all:
mailer:
param:
transport:
class: Swift_SmtpTransport
param:
host: smtp.gmail.com
port: 465
encryption: ssl
username: myemail#gmail.com
password: password
will it effect anything ? or what's the original on top does?
Why remove the previous values? Just merge the two files, like this:
all:
routing:
class: sfPatternRouting
param:
generate_shortest_url: true
extra_parameters_as_query_string: true
view_cache_manager:
class: sfViewCacheManager
param:
cache_key_use_vary_headers: true
cache_key_use_host_name: true
mailer:
param:
transport:
class: Swift_SmtpTransport
param:
host: smtp.gmail.com
port: 465
encryption: ssl
username: myemail#gmail.com
password: password
otherwise the settings for the routing and the view_cache_manager will be the one you can find on that other factories.yml somewhere in lib/vendor/symfony
I use symfony in version 1.4.9 with doctrine.
symfony doctrine:build-schema works and create a schema in config/doctrine/schema.yml
symfony doctrine:build-model fails with the following message:
>> doctrine generating model classes
>> file+ /tmp/doctrine_schema_92251.yml
Invalid schema element named "class" at path "doctrine"
the tmp/doctrine_schema_922... is this:
doctrine:
class: sfDoctrineDatabase
param:
dsn: 'mysql:host=localhost;dbname=xxxxx'
username: xxxxx
password: xxxxx
attributes:
use_dql_callbacks: true
use_native_enum: true
Products:
connection: doctrine
tableName: products
columns:
product_id:
type: integer
...
any ideas whats wrong?
I don't think the first section of your schema is valid, I have never seen this before.
doctrine:
class: sfDoctrineDatabase
param:
dsn: 'mysql:host=localhost;dbname=xxxxx'
username: xxxxx
password: xxxxx
attributes:
use_dql_callbacks: true
use_native_enum: true
The database connection string should go in your database.yml file. In fact, this whole section resembles something that would go in the database.yml file. The remaining code in your schema looks good.
is possible overwriting a Doctrine model in Symfony?
I'm trying no change a "notnull" property, but i can get it..
In 'plugins/sfDoctrineGuardPlugin/config/doctrine/schema.yml':
sfGuardUser:
actAs: [Timestampable]
columns:
id:
type: integer(4)
primary: true
autoincrement: true
username:
type: string(128)
notnull: true
unique: true
#...
And in 'config/doctrine/schema.yml':
sfGuardUser:
columns:
username:
type: string(128)
notnull: false
unique: true
Then "build-all-reload" but it doesn't change..
Any idea?
Javi
As coronatus said, you can overwrite schemas in Symfony 1.4, you should be able to do this in Symfony 1.3 also if you want to upgrade your project without removing all deprecated 1.2 stuff.