I want to create API from my plugin but API returns all I need expect image paths. Do you know anyone how to get image paths and return in my API?
In my routes.php I have
<?php
use Mycompany\ArubaPlaces\Models\Places;
Route::get('api/places', function(){
$places = Places::All();
return $places;
});
And this is my modelI have this relation. I need "placeimages" paths in my API:
public $attachMany = [
'placesimages' => 'System\Models\File'
];
fields.yaml
fields:
name:
label: Title
span: auto
required: 1
type: text
slug:
label: Slug
span: auto
preset:
field: name
type: slug
type: text
description:
label: Description
size: small
oc.commentPosition: ''
span: full
type: richeditor
latitude:
label: Latitude
span: auto
required: 1
type: text
longitude:
label: Longitude
span: auto
required: 1
type: text
placesimages:
label: 'Place images'
mode: image
useCaption: true
thumbOptions:
mode: crop
extension: auto
span: full
type: fileupload
active:
label: AktĂvne
span: auto
type: switch
1) First option is adding protected $with = ['placesimages']; to your Places model to eager load the desired relationships, here you can add more than one relation by default.
2) You can do the eager loading in your query e.g Places::with('placesimages')->get(); if you do not want to eager load all files by default. Also here with accepts an array of relations.
If you are sure that in your app everytime you query a place you want to fetch the images then go for option one, otherwise, option 2 will only get the images when only you specify it in the query builder.
Read more about eager loading
Related
I have a several models that reference each other. Here is an example.
export const AlbumModel = types
.model("Album")
.props({
id: types.identifier,
name: types.string,
artists: types.array(ArtistModel),
uri: types.string,
releaseDate: types.maybe(types.string),
images: types.array(types.maybe(ImageModel))
})
export const ArtistModel = types
.model("Artist")
.props({
id: types.identifier,
name: types.string,
uri: types.string
})
export const ImageModel = types
.model("Image")
.props({
url: types.identifier,
width: types.maybe(types.number),
height: types.maybe(types.number)
})
Then, when I go to create an album, I do something like this:
Album.create({
id: '12345',
name: 'My Cool Album',
artists: [{
id: '67890',
name: 'Mr. Bean',
uri: 'https://my-cool-site.com/artist/mr-bean'
}],
uri: 'https://my-cool-site.com/album/my-cool-album',
releaseDate: '12-12-2012',
images: [{
url: 'https://my-cool-site.com/pic/1'
}]
})
This works fine, but when I fetch data from a service, I get a bunch of JSON I want to stick into the mst. When I do that as is, I am creating duplicate artists and images.
Question
Is there a (simple-ish) way I can create the artists and images on the fly, and pass them as reference to the Album model. Or do I need to write a separate function that will create the images and artists, stick them into the tree, and then reference them as a part of the album?
What I tried:
export const AlbumModel = types
.model("Album")
.props({
id: types.identifier,
name: types.string,
artists: types.array(types.reference(ArtistModel)),
uri: types.string,
releaseDate: types.maybe(types.string),
images: types.array(types.maybe(types.reference(ImageModel)))
})
which throws the following error:
at path "/images/0" snapshot `{"height":300,"url":"https://my-cool-site.com/pic/1","width":300}` is not assignable to type: `(reference(Image) | undefined)` (No type is applicable for the union), expected an instance of `(reference(Image) | undefined)` or a snapshot like `(reference(Image) | undefined?)` instead.
Not sure if this will work but it's worth trying
If you put this in AlbumModel artists
types.reference(ArtistModel, {
// given an identifier, find the user
get(artist /* string */, parent: any /*AlbumModel*/) {
return parent.artists.find(a => a.id === artist.id) || ArtistModel.create(data)
},
// given a user, produce the identifier that should be stored
set(artist /* ArtistModel */) {
return artist.id
}
})
The idea is initially it tries to find if the artist already exists, if not it will create a new ArtistModel.
Please let me know if this works :)
i am trying to change field label dynamically base on username that starts with specific string
public function filterFields($fields,$context = null)
{
$nager = WartaDataProfil::where('backend_users_id',$this->getBackendUserId())->pluck('nager');
if(Str::startsWith($nager[0],'GP')){
$fields->wil->label = "Sektor";
}
}
i got error
Creating default object from empty value
from line
$fields->wil->label = "Sektor";
when i dd($fields->wil->label); i got default string which is defined in yaml file..
fields.yaml
fields:
nowarta:
label: 'No. Warta'
...
tanggal:
label: 'Tanggal'
...
batasno:
label: ''
...
type: section
tabs:
fields:
renung:
label: 'Renungan Mingguan'
span: full
type: section
tab: 'Renungan/Artikel'
renungan:
label: ''
...
type: repeater
...
minItems: '1'
maxItems: '1'
form:
fields:
judul:
label: 'Judul'
...
bacaan:
label: 'Bahan'
...
penulis:
label: 'Penulis'
...
isi:
label: Renungan
...
keb:
label: 'Kebaktian Umum'
span: full
type: section
tab: 'Kebaktian'
kebakti:
label: ''
span: full
type: partial
path: field_kebum
tab: 'Kebaktian'
kom:
label: 'Kebaktian Komisi'
span: left
type: section
tab: 'Kebaktian'
wil: <<< THIS FIELD <<<<<<<<<<<<<<<<<
label: 'Persekutuan Wilayah'
span: right
type: section
tab: 'Kebaktian'
Komisi:
label: ''
span: left
type: partial
path: field_kebkom
tab: 'Kebaktian'
pwil:
label: ''
span: right
type: partial
path: field_pwilayah
tab: 'Kebaktian'
hope this help to get solution for my problem.. because i use the same filterField in other model and its working fine..
I am currently working at October CMS plugin development, I have a problem with file upload. I put the fileupload type to file_src. When I go to to resource it shows an error like this :
model/resource
fields:
name:
label: Name
type: text
required: true
file_src:
label: Upload
type: fileupload
grade_id:
label: Grade
type: dropdown
emptyOption: Select
showSearch: true
subject_id:
label: Subject
type: dropdown
emptyOption: Select
showSearch: true
type_id:
label: Type
type: dropdown
emptyOption: Select
showSearch: true
How can I fix this error?
I fixed it! We must use $attachOne or $attachMany relation on model
I added following code in Resource.php in models
public $attachOne = [
'file_src' => 'System\Models\File'
];
thats it its working now!
Define the current module the selected control is a file. Mean your
current selected control is as a file and you does not define in your
module that time the backend has confused.
Solution:
public $attachOne = [
'profile_pic' => \System\Models\File::class
];
I use propelorm (propel 1.6) plugin and symfony 1.4.20.
I have book table with I18n, and this book have relation with gallery table which also have I18n. I fill all book form fields, added two gallery images, filled all images fields. Click on save - it seems all ok: book fields are ok, first and second image have been uploaded, but only first image have filled i18n fields, second image all i18n fields are empty. What's the prob ?
(there's no redeclared doSave(), save(), bind() and other form methods)
schema:
book:
id: ~
title: { type: varchar(255), required: true }
author: { type: varchar(255), required: true }
description: { type: longvarchar, required: true }
sortable_rank: { type: integer, required: true }
is_active: { type: boolean, default: 1 }
_indexes:
active_sort: [ is_active, sortable_rank ]
_propel_behaviors:
sortable:
rank_column: sortable_rank
i18n:
i18n_columns: [title, description, author]
locale_column: culture
locale_alias: culture
default_locale: en
book_gallery:
id: ~
book_id: { type: integer, foreignTable: book, foreignReference: id, required: true, onDelete: cascade }
title: { type: varchar(255), required: true }
image: { type: varchar(255), required: true }
_propel_behaviors:
i18n:
i18n_columns: [title]
locale_column: culture
locale_alias: culture
default_locale: en
book form:
$languages = LanguageQuery::create()->getAllAsArray(); // returns like array('en' => 'english', ...);
$this->embedI18n(array_keys($languages));
$this->widgetSchema->setLabels($languages);
$this->embedRelation('BookGallery', array(
'title' => 'Application book store list',
'item_pattern' => 'Application book store %index%',
));
book gallery form:
$languages = LanguageQuery::create()->getAllAsArray(); // same as in book form
$this->embedI18n(array_keys($languages));
$this->widgetSchema->setLabels($languages);
I have a database design case which I am curios whether Doctrine ORM supports it out-of-box.
Product:
columns:
id: {type: integer, primary: true, autoincrement: true }
type_id: { type: integer, notnull: true }
brand_id: { type: integer, notnull: true }
relations:
ProductType:
class: ProductType
local: type_id
foreign: id
Brand:
class: Brand
local: brand_id
foreign: id
ProductType:
actAs:
I18n:
fields: { name }
columns:
id: {type: integer, primary: true, autoincrement: true }
name: { type: string(255), notnull: true }
Brand:
actAs:
I18n:
fields: { name }
columns:
id: {type: integer, primary: true, autoincrement: true }
name: { type: string(255), notnull: true }
I want to slugify Products table, ie. products will be reached via their slugs. However, as you see both brand and productype tables has i18n behaviour. And moreover, product doesnt have a name. A product's slug will be: "Brand.name - ProductType.name", and vary with the language served.
For this scenario, is there anyway I can use Sluggable behaviour of Doctrine to sluggify my products automatically. Or do I have to manage it manually?
By the way my environment configuration is:
Doctrine Version: 1.2
Symfony: 1.4.1
Thanks
My understanding is that you need to have the slug in both Product Type and Brand models. You can leave the Product definition as it is. Anyway, I'm assuming from your question that there is only one product for every brand+type (even if it doesn't have to much sense). So ProductType and Brand will be defined like this:
schema.yml
----------
ProductType:
actAs:
I18n:
fields: { name }
actAs:
Sluggable: { fields: [name], uniqueBy: [lang], canUpdate: true }
columns:
...
Then you have to configure your Product route to use the slugs. And after that you will need to configure the action to check what you are getting from the route.
For example, this could be your route for Products:
routing.yml
-----------
product:
url: /:sf_culture/product/:brand_slug/:type_slug
param: { module: product, action: view }
requirements:
sf_culture: (?:en|fr)
sf_method: get
Then in the action you will call to your own findOneBySlugs($brand_slug, $type_slug) method:
product/actions/actions.class.php
---------------------------------
public function executeView(sfWebRequest $request)
{
$product = Doctrine::getTable('Product')
->findOneBySlugs(
$request->getParameter('brand_slug'),
$request->getParameter('type_slug')
);
$this->forward404Unless($product);
}
the problem with that solution are the queries. With:
$product = Doctrine::getTable('Product')
->findOneBySlugs(
$request->getParameter('brand_slug'),
$request->getParameter('type_slug')
);
you are doing a 5-join query if I'm not mistaken. You can improve to do only three (product, brand_translation and producttype_translation)
I'm in a similar situation, and the best option is to create a slug for each product using the brand or product type name in this case. So you would only need:
$product = Doctrine::getTable('Product')
->findOneBySlug($request->getParameter('slug'));
I'm thinking between two options:
Product:
actAs:
Sluggable:
unique: true
fields: [title]
builder: [Slug, slugify]
or using the getUniqueSlug() function on the record class. I think the first option is best so you don't have to worry about uniqueness.