Symfony relations between tables - model-view-controller

I use symfony 1.4.10
I have next situation:
User can create "Ads" and he can create one "company". My part of schema:
Ads:
actAs:
Timestampable: ~
Sluggable:
unique: true
fields: [title]
canUpdate: false
connection: doctrine
tableName: ads
columns:
ad_id: { type: integer(4), primary: true, autoincrement: true, notnull: true }
user_id: {type: int(4) }
country: {type: string(255), notnull: true }
category_id: { type: int(4), notnull: true }
company: {type: string(255), notnull: true }
address: {type: string(255), notnull: true }
contact_person: {type: string(255), notnull: true }
phone: { type: string(50), notnull: true }
fax: { type: string(50) }
email: {type: string(255), notnull: true}
show_in_profile: { type: boolean,notnull: true , default: false }
title: {type: string(255), notnull: true }
content: {type: string(), notnull: true }
company: {type: string(255), notnull: true }
AGB: { type: boolean,notnull: true , default: false }
active: { type: boolean,notnull: true , default: 0 }
expires_at: { type: timestamp, notnull: true }
relations:
Owner: { onDelete: CASCADE, local: user_id, foreign: id, class: sfGuardUser }
Bookmark: { local: ad_id, foreign: user_id, class: sfGuardUser, refClass: Bookmarks }
Categories: { onDelete: CASCADE, local: ad_id, foreign: category_id }
Images: { local: ad_id, foreign: ad_id, type: many, class: Images }
Companies:
actAs:
Timestampable: ~
Sluggable:
unique: true
fields: [company]
canUpdate: false
connection: doctrine
tableName: companies
columns:
company_id: { type: integer(4), primary: true, notnull: true, autoincrement: true }
user_id: {type: int(4) }
category_id: {type: int(4), notnull: true }
company: {type: string(255), notnull: true }
address: {type: string(255), notnull: true }
contact_person: {type: string(255), notnull: true }
phone: { type: string(50), notnull: true }
fax: { type: string(50) }
email: {type: string(255), notnull: true}
url: { type: string(50) }
about: {type: string(), notnull: true}
country: {type: string(255), notnull: true}
active: { type: boolean, default: 0 }
has_company: { type: boolean, default: 1 }
relations:
Owner: { onDelete: CASCADE, local: user_id, foreign: id, class: sfGuardUser }
Images_companies: { local: company_id, foreign: company_id, type: many, class: Images_companies }
Categories: { onDelete: CASCADE, local: company_id, foreign: category_id }
sfGuardUserProfile:
connection: doctrine
tableName: sf_guard_user_profile
columns:
id: { type: integer(4), primary: true, autoincrement: true }
user_id: { type: integer(4), notnull: true }
salutation: { type: string(10), notnull: true }
first_name: { type: string(30), notnull: true }
last_name: { type: string(30), notnull: true }
country: { type: string(255), notnull: true }
postcode: { type: string(10) , notnull: true }
city: { type: string(255), notnull: true }
address: { type: string() , notnull: true }
phone: { type: string(50) }
email: { type: string(255), notnull: true }
validate: { type: string(17) }
banned: { type: boolean, default: 0 }
payed_until: { type: datetime, notnull: true}
relations:
User:
class: sfGuardUser
local: user_id
foreign: id
type: one
foreignType: one
foreignAlias: Profile
onDelete: CASCADE
So I need in each Ads of user put link to company of this user.So I do not know how to do it...
Part of routing:
ads:
url: /:sf_culture/account/ads/:action/*
param: { module: ads }
requirements:
sf_culture: (?:de|en|ru)
companies:
url: /:sf_culture/account/company/:action/*
param: { module: companies }
requirements:
sf_culture: (?:de|en|ru)
I think that it is possible in each ad to form a link to a company, but I do not know how I can get the id of company what I need.probably add relations between "Ads" and "Company"?Maybe there is another way to make a link to company?
p.s Sorry for ma bad English

that should be easy!
go to the form, and add this to the configure() method
public function configure() {
parent::configure();
unset(
$this['company_id'],
);
}
And when the form is saved, handle this in the action:
if ($this->form->isValid()) {
$user_id = // get user_id of authenticated user
$user = // get user obj
$company_id = $user->Companies->id;
$temp_ad = $this->form->getObject();
$ad->company_id = $company_id ;
$this->form->save();
}
Also, in your Ads model, the relation for Companies, foreign_id should just be id

In the companies model, put a unique index on user_id. That way a user can create only one company
Also, in companies model,
Owner: { onDelete: CASCADE, local: user_id, foreign: id, class: sfGuardUser, foreignAlias: Company }
Now in code, you can always do $user->Company to get the user's primary company
Create a reference to Company in the Ads model
Company: { onDelete: CASCADE, local: company_id, foreign: id, class: Company, foreignAlias: Ads }
Now you can get all Ads for a company as $company->Ads
or for a user $user->Company->Ads

Related

Matrix Synapse server: Filtering Events for messages only (m.room.message), can't get it to work

I call the Synapse method
https://spec.matrix.org/v1.3/client-server-api/#get_matrixclientv3roomsroomidmessages
and want to get only the messages, and I am using specific filter but I get also other events than messages
Steps to reproduce:
call the Synapse API method
http://synapse:8008/_matrix/client/v3/rooms/!EoDQvlifoBggEjcJeT:matrix.connectme.anais.tech/messages?dir=b&filter=%7B%22type%22%3A%22m.room.message%22%7D&user_id=#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech
get back answer
{
type: 'm.room.message',
room_id: '!EoDQvlifoBggEjcJeT:matrix.connectme.anais.tech',
sender: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
content: { body: 'Hello', msgtype: 'm.text' },
origin_server_ts: 1660060721216,
unsigned: { age: 88017942 },
event_id: '$8GII9nJiCKPpPm7DqviMRxq7zsbPNieH8R-uJlCFQ3Q',
user_id: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
age: 88017942
},
{
type: 'm.room.member',
room_id: '!EoDQvlifoBggEjcJeT:matrix.connectme.anais.tech',
sender: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
content: {
reason: 'To chat',
membership: 'join',
displayname: 'sofia#coppensint.com'
},
state_key: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
origin_server_ts: 1660060721176,
unsigned: { age: 88017982 },
event_id: '$tc0PAcZmdkh4tt0WxNkEmg5B9wTBk1FBMRud7TEMCa8',
user_id: 'ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
age: 88017982
},
{
type: 'm.room.topic',
room_id: '!EoDQvlifoBggEjcJeT:matrix.connectme.anais.tech',
sender: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
content: {
topic: 'Topic Room 1 to 1 ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech to ge1jpwwv9dtu2wrvfdm2:matrix.connectme.anais.tech'
},
state_key: '',
origin_server_ts: 1660060721126,
unsigned: { age: 88018032 },
event_id: '$aOyVdr9P8_cfNHKDdPKumLOrp-kQK3AWc4dJqdTuEQg',
user_id: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
age: 88018032
},
..................
observe there are also other event types besides m.room.message
Homeserver: local homeserver
Synapse Version:
{"server_version":"1.61.1","python_version":"3.9.13"}
Installation Method:
Docker (matrixdotorg/synapse)
Platform:
Ubuntu running docker, and in docker Synapse is running.
Relevant log output:
{
type: 'm.room.message',
room_id: '!EoDQvlifoBggEjcJeT:matrix.connectme.anais.tech',
sender: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
content: { body: 'Hello', msgtype: 'm.text' },
origin_server_ts: 1660060721216,
unsigned: { age: 88017942 },
event_id: '$8GII9nJiCKPpPm7DqviMRxq7zsbPNieH8R-uJlCFQ3Q',
user_id: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
age: 88017942
},
{
type: 'm.room.member',
room_id: '!EoDQvlifoBggEjcJeT:matrix.connectme.anais.tech',
sender: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
content: {
reason: 'To chat',
membership: 'join',
displayname: 'sofia#coppensint.com'
},
state_key: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
origin_server_ts: 1660060721176,
unsigned: { age: 88017982 },
event_id: '$tc0PAcZmdkh4tt0WxNkEmg5B9wTBk1FBMRud7TEMCa8',
user_id: 'ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
age: 88017982
},
{
type: 'm.room.topic',
room_id: '!EoDQvlifoBggEjcJeT:matrix.connectme.anais.tech',
sender: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
content: {
topic: 'Topic Room 1 to 1 ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech to ge1jpwwv9dtu2wrvfdm2:matrix.connectme.anais.tech'
},
state_key: '',
origin_server_ts: 1660060721126,
unsigned: { age: 88018032 },
event_id: '$aOyVdr9P8_cfNHKDdPKumLOrp-kQK3AWc4dJqdTuEQg',
user_id: '#ge1jpwwv9dtu2wrvfdm:matrix.connectme.anais.tech',
age: 88018032
},
the filter parameter supports a key types of type string[];
example:
"&filter=" + encodeURIComponent(JSON.stringify(
{
types: ["m.room.message"],
}
))

What is the correct way to nest data within a graphql?

I have an address in my database that I've put into a location hash. The hash contains separate keys for streetAddress, city, state, and zipCode. I've nested the data like so in my graphql schema file:
location: {
streetAddress: {
type: String,
required: true,
unqiue: true
},
city: {
type: String,
required: true
},
state: {
type: String,
required: true
},
zipCode: {
type: Number,
required: true
}
}
And I've implemented the schema type like this:
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
id: {type: GraphQLID},
phoneNum: { type: GraphQLString },
location: {
streetAddress: { type: GraphQLString },
city: { type: GraphQLString },
state: { type: GraphQLString },
zipCode: { type: GraphQLInt }
}
...
However, I get an error message saying that the output type is undefined when I try to do a query in graphql:
"message": "The type of RestaurantType.location must be Output Type but got: undefined."
I believe I understand where the error is coming from; I'm assuming that it expects location to have a type as well. What would be the correct syntax for doing this/fixing this error message?
As you guessed, you cannot have nested fields like that. You need to create a separate type for every object in your schema. First create the type:
const Location = new GraphQLObjectType({
name: 'Location',
fields: () => ({
streetAddress: { type: GraphQLString },
city: { type: GraphQLString },
state: { type: GraphQLString },
zipCode: { type: GraphQLInt }
}),
})
Then use it:
const Restaurant = new GraphQLObjectType({
name: 'Restaurant',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
location: { type: Location },
}),
})
or if you don't need to reuse the type, you can define it inline like this:
const Restaurant = new GraphQLObjectType({
name: 'Restaurant',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
location: {
type: new GraphQLObjectType({
name: 'Location',
fields: () => ({
streetAddress: { type: GraphQLString },
city: { type: GraphQLString },
state: { type: GraphQLString },
zipCode: { type: GraphQLInt }
}),
})
},
}),
})

Validate datetime in Strapi

I create DateTime picker in Strapi with the required field validation but can't save. occurring following error.
apI Parametr
components: []
contentType: {name: "Events", description: "", connection: "default", collectionName: "events",…}
name: "Events"
description: ""
connection: "default"
collectionName: "events"
attributes: {title: {type: "string", required: true},…}
title: {type: "string", required: true}
created_by: {nature: "oneWay", target: "plugins::users-permissions.user", dominant: false, unique: false}
image: {type: "media", multiple: false, required: true}
seo_description: {type: "text"}
seo_title: {type: "string"}
description: {type: "richtext", required: true}
calendly_url: {type: "string"}
start_date: {type: "datetime", required: true}
type: "datetime"
required: true
end_date: {type: "datetime"}
PUT http://localhost:1337/content-type-builder/content-types/application::events.events 400 (Bad Request)```

GraphQL + Sequalize + existing database - "message": "parent.getPages is not a function" for one model not the other

GraphQL Query
Morning all,
I have a nice setup GraphQL -> Sequalize -> Existing DB (generated by a Laravel application). I've built this schema out:
type App {
id: ID!
userId: ID
user: User
pages: [Page]
experiments: [Experiment]
uri: String!
createdAt: String!
updatedAt: String!
}
type Page {
id: ID!
userId: ID
user: User
appId: ID!
app: App!
uri: String!
createdAt: String!
updatedAt: String!
}
type Experiment {
id: ID!
title: String!
appId: ID!
app: App!
createdAt: String!
updatedAt: String!
}
Based on the existing data. Querying an apps experiments works just great:
query {
app(id: 6) {
id
title
experiments {
id
}
}
}
{
"data": {
"app": {
"id": "6",
"title": "C-Map Embark: Boating",
"experiments": [
{
"id": "1"
}
]
}
}
}
But querying pages I get this:
query {
app(id: 6) {
id
title
pages {
id
}
}
}
{
"errors": [
{
"message": "parent.getPages is not a function",
"locations": [
{
"line": 5,
"column": 5
}
],
"path": [
"app",
"pages"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"TypeError: parent.getPages is not a function",
...
The db columns are the same, as are the resolvers:
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
const Page = sequelize.define(
"page",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
...
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
},
{
tableName: "pages",
underscored: true
}
);
Page.associate = models => {
Page.belongsTo(models.app);
};
return Page;
};
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
const Experiment = sequelize.define(
"experiment",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
...
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
},
{
tableName: "experiments",
underscored: true
}
);
Experiment.associate = models => {
Experiment.belongsTo(models.app);
};
return Experiment;
};
Have you come across this before?

Problem with symfony2 schema yaml: unable to get relationships working

I have two entities: User and Student. Following is the yaml schema:
ABC\UserBundle\Entity\User:
type: entity
table: abc_user
id:
id: { type: integer, generator: { strategy: AUTO } }
fields:
username: { type: string, length: 255, notnull: true, unique: true }
email: { type: string, length: 255, notnull: true, unique: true }
password: { type: string, length: 255, notnull: true }
enabled: { type: boolean }
ABC\UserBundle\Entity\Student:
type: entity
table: abc_student
id:
id: { type: integer, generator: { strategy: AUTO } }
fields:
first_name: { type: string, length: 255, notnull: true }
middle_name: { type: string, length: 255 }
last_name: { type: string, length: 255, notnull: true }
OnetoOne:
user:
targetEntity: ABC\UserBundle\Entity\User
My problem is that when I do "doctine:update:schema --dump-sql", user_id field is not added to the Student table and no relationship is created between the entities.
Yaml map is case-sensitive, use the proper name oneToOne for creating relations.
Your are missing the joinColumn option for the OneToOne association:
ABC\UserBundle\Entity\User:
type: entity
table: abc_user
id:
id: { type: integer, generator: { strategy: AUTO } }
fields:
username: { type: string, length: 255, notnull: true, unique: true }
email: { type: string, length: 255, notnull: true, unique: true }
password: { type: string, length: 255, notnull: true }
enabled: { type: boolean }
OnetoOne:
student:
targetEntity: ABC\UserBundle\Entity\Student
mappedBy: user
ABC\UserBundle\Entity\Student:
type: entity
table: abc_student
id:
id: { type: integer, generator: { strategy: AUTO } }
fields:
first_name: { type: string, length: 255, notnull: true }
middle_name: { type: string, length: 255 }
last_name: { type: string, length: 255, notnull: true }
OnetoOne:
user:
targetEntity: ABC\UserBundle\Entity\User
joinColumn:
name: user_id
referencedColumnName: id

Resources