SubQuery in ActiveRecord - yii2-advanced-app

i have following models in yii2:
use frontend\modules\bewerber\models\Bewerber;
use common\modules\basis\models\base\Person;
use common\modules\lookup\models\LAnrede;
How to create following query using methods of ActiveRecord?
SELECT anrede FROM L_anrede JOIN Person ON L_anrede.id=Person.id_anrede WHERE Person.id IN
(SELECT id_person FROM Bewerber WHERE Bewerber.id_person=1);
P.S.: The last WHERE clause should be not fix but variable like this:
var_dump(LAnrede::findOne([$model->id_person])->anrede)
which will put out following result:Mister or Miss
................................................................
Hint for Fabrizio Caldarelli
................................................................
Ur solution won't help me:=(
This is ur code:
$idPerson = 1;
$show=LAnrede::find()->joinWith(['Person' => function($q) use($idPerson) {
$q->andWhere([
'Person.id' => (new \yii\db\Query())->from('Bewerber')->where(['Bewerber.id_person' => $idPerson])
])->anrede;
}]);
and this is var_dump($show);
E:\xampp\htdocs\yii2_perswitch\frontend\modules\bewerber\views\bewerber\index.php:48:
object(common\modules\lookup\models\LAnredeQuery)[207]
public 'sql' => null
public 'on' => null
public 'joinWith' =>
array (size=1)
0 =>
array (size=3)
0 =>
array (size=1)
...
1 => boolean true
2 => string 'LEFT JOIN' (length=9)
public 'select' => null
public 'selectOption' => null
public 'distinct' => null
public 'from' => null
public 'groupBy' => null
public 'join' => null
public 'having' => null
public 'union' => null
public 'params' =>
array (size=0)
empty
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
public 'where' => null
public 'limit' => null
public 'offset' => null
public 'orderBy' => null
public 'indexBy' => null
public 'emulateExecution' => boolean false
public 'modelClass' => string 'common\modules\lookup\models\LAnrede' (length=36)
public 'with' => null
public 'asArray' => null
public 'multiple' => null
public 'primaryModel' => null
public 'link' => null
public 'via' => null
public 'inverseOf' => null
I use Gridview like this
$gridColumn = [
[
'attribute' => '',
'label' => Yii::t('app', 'Anrede'),
'format' => 'html',
'value' => function($model) {
return "<p><font color='green'>" . LAnrede::findOne([$model->id_person])->anrede . "</p>";
}
],
];
Colud u show me up how to use ur solution in this context?

This should work:
$idPerson = 1;
LAnrede::find()->joinWith(['Person' => function($q) use($idPerson) {
$q->andWhere([
'Person.id' => (new \yii\db\Query())->from('Bewerber')->where(['Bewerber.id_person' => $idPerson])
]);
}])
->all();
'Person' is a relation in LAnrede model (one or many relation?)
public function getPerson()
{
return $this->hasMany(Person::className(), ['id_anrede' => 'id']);
}

Related

Exclude fields from a Resource template Laravel 7 and 8

I have come across a solution that filters fields from a resource collection in the Controller CompanyController.php
E.g The code below returns all the values except company_logo
CompanyResource::collection($companies)->hide(['company_logo']);
CompanyResource.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class CompanyResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
protected $withoutFields = [];
public static function collection($resource)
{
return tap(new CompanyResourceCollection($resource), function ($collection) {
$collection->collects = __CLASS__;
});
}
// Set the keys that are supposed to be filtered out
public function hide(array $fields)
{
$this->withoutFields = $fields;
return $this;
}
// Remove the filtered keys.
protected function filterFields($array)
{
return collect($array)->forget($this->withoutFields)->toArray();
}
public function toArray($request)
{
return $this->filterFields([
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'telephone' => $this->telephone,
'company_logo' => $this->company_logo,
'social_links' => $this->social_links,
]);
}
}
Now from my UserResource I still want to specify fields I don't want returned from the same CompanyResource but it's not a collection anymore in the UserResource
UserResource.php
public function toArray($request)
{
return [
'id' => $this->id,
'email' => $this->email,
'status' => $this->status,
'timezone' => $this->timezone,
'last_name' => $this->last_name,
'first_name' => $this->first_name,
'tags' => TagResource::collection($this->whenLoaded('tags')),
'company' => new CompanyResource($this->whenLoaded('company')),
];
}
So my idea is to be able to specify excluded fields on 'company' => new CompanyResource($this->whenLoaded('company')), Been stuck here for some time.
After researching I found a working solution for my problem
'company' => CompanyResource::make($this->whenLoaded('company'))->hide(['company_logo']),
Instead of the below which I could not use flexibly use:
'company' => new CompanyResource($this->whenLoaded('company')),

Symfony3.3 form name not in Request

Heads up - I've already been here. Strange issue with Symfony3 forms. So I've created FormType class:
class GetPostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod("POST")
->add('phrase', Type\TextType::class);
}
}
As you can see form is not bound to any entity or other object(this shouldn't matter though). Now when I try to handle incoming request with that form:
$postForm = $this->createForm(GetPostType::class);
$postForm->handleRequest($this->request);
... normally handleRequest should submit my form. However:
$postForm->isSubmitted();
is false.
I've tracked down source of issue to HttpFoundationRequestHandler.
} elseif ($request->request->has($name) || $request->files->has($name)) {
According to this my form name is not present in request. That's what I don't get. I'm using Symfony forms at work and there's no problem. I used them in different projects and it was ok. Now for some reason I can't get them to work. It is probably something obvious but I guess I need another pair of eyes to see it.
As you can see I did try to set form method manually(it should be POST by default) but it didn't work. I did clear the cache and stuff. I did try different things I found on the internet but no luck.
I can't find anything that might be the reason. Any ideas what it might be?
UPDATE:
As requested in comments section I provide dump of my request object.
So how do I know it is the right thing? It is accepted by handleRequest method witch expects request type and it contains correct data.
/var/www/story/src/CodeCraft/BlogBundle/Controller/Api/PostController.php:73:
object(Symfony\Component\HttpFoundation\Request)[49]
public 'attributes' =>
object(Symfony\Component\HttpFoundation\ParameterBag)[12]
protected 'parameters' =>
array (size=4)
'_controller' => string 'CodeCraft\BlogBundle\Controller\Api\PostController::getPostsAction' (length=66)
'_route' => string 'blog.api.posts.get' (length=18)
'_route_params' =>
array (size=0)
...
'_firewall_context' => string 'security.firewall.map.context.main' (length=34)
public 'request' =>
object(Symfony\Component\HttpFoundation\ParameterBag)[10]
protected 'parameters' =>
array (size=1)
'phrase' => string 'xxx' (length=3)
public 'query' =>
object(Symfony\Component\HttpFoundation\ParameterBag)[11]
protected 'parameters' =>
array (size=0)
empty
public 'server' =>
object(Symfony\Component\HttpFoundation\ServerBag)[15]
protected 'parameters' =>
array (size=34)
'HTTP_CACHE_CONTROL' => string 'no-cache' (length=8)
'HTTP_POSTMAN_TOKEN' => string '2eea2285-a2f7-4ca5-a799-ea97758d7d20' (length=36)
'CONTENT_TYPE' => string 'application/x-www-form-urlencoded' (length=33)
'HTTP_USER_AGENT' => string 'PostmanRuntime/6.1.6' (length=20)
'HTTP_ACCEPT' => string '*/*' (length=3)
'HTTP_HOST' => string 'story.dev' (length=9)
'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate' (length=13)
'CONTENT_LENGTH' => string '10' (length=2)
'HTTP_CONNECTION' => string 'keep-alive' (length=10)
'PATH' => string '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' (length=60)
'SERVER_SIGNATURE' => string '<address>Apache/2.4.18 (Ubuntu) Server at story.dev Port 80</address>
' (length=70)
'SERVER_SOFTWARE' => string 'Apache/2.4.18 (Ubuntu)' (length=22)
'SERVER_NAME' => string 'story.dev' (length=9)
'SERVER_ADDR' => string '192.168.33.10' (length=13)
'SERVER_PORT' => string '80' (length=2)
'REMOTE_ADDR' => string '192.168.33.1' (length=12)
'DOCUMENT_ROOT' => string '/var/www/story/web' (length=18)
'REQUEST_SCHEME' => string 'http' (length=4)
'CONTEXT_PREFIX' => string '' (length=0)
'CONTEXT_DOCUMENT_ROOT' => string '/var/www/story/web' (length=18)
'SERVER_ADMIN' => string 'webmaster#localhost' (length=19)
'SCRIPT_FILENAME' => string '/var/www/story/web/app_dev.php' (length=30)
'REMOTE_PORT' => string '57039' (length=5)
'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
'REQUEST_METHOD' => string 'POST' (length=4)
'QUERY_STRING' => string '' (length=0)
'REQUEST_URI' => string '/app_dev.php/posts' (length=18)
'SCRIPT_NAME' => string '/app_dev.php' (length=12)
'PATH_INFO' => string '/posts' (length=6)
'PATH_TRANSLATED' => string '/var/www/story/web/posts' (length=24)
'PHP_SELF' => string '/app_dev.php/posts' (length=18)
'REQUEST_TIME_FLOAT' => float 1509896560.734
'REQUEST_TIME' => int 1509896560
public 'files' =>
object(Symfony\Component\HttpFoundation\FileBag)[14]
protected 'parameters' =>
array (size=0)
empty
public 'cookies' =>
object(Symfony\Component\HttpFoundation\ParameterBag)[13]
protected 'parameters' =>
array (size=0)
empty
public 'headers' =>
object(Symfony\Component\HttpFoundation\HeaderBag)[16]
protected 'headers' =>
array (size=10)
'cache-control' =>
array (size=1)
...
'postman-token' =>
array (size=1)
...
'content-type' =>
array (size=1)
...
'user-agent' =>
array (size=1)
...
'accept' =>
array (size=1)
...
'host' =>
array (size=1)
...
'accept-encoding' =>
array (size=1)
...
'content-length' =>
array (size=1)
...
'connection' =>
array (size=1)
...
'x-php-ob-level' =>
array (size=1)
...
protected 'cacheControl' =>
array (size=1)
'no-cache' => boolean true
protected 'content' => null
protected 'languages' => null
protected 'charsets' => null
protected 'encodings' => null
protected 'acceptableContentTypes' => null
protected 'pathInfo' => string '/posts' (length=6)
protected 'requestUri' => string '/app_dev.php/posts' (length=18)
protected 'baseUrl' => string '/app_dev.php' (length=12)
protected 'basePath' => null
protected 'method' => string 'POST' (length=4)
protected 'format' => null
protected 'session' =>
object(Symfony\Component\HttpFoundation\Session\Session)[5070]
protected 'storage' =>
object(Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage)[5071]
protected 'bags' =>
array (size=2)
...
protected 'started' => boolean false
protected 'closed' => boolean false
protected 'saveHandler' =>
object(Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy)[5090]
...
protected 'metadataBag' =>
object(Symfony\Component\HttpFoundation\Session\Storage\MetadataBag)[5065]
...
private 'flashName' => string 'flashes' (length=7)
private 'attributeName' => string 'attributes' (length=10)
protected 'locale' => null
protected 'defaultLocale' => string 'en' (length=2)
private 'isHostValid' => boolean true
private 'isClientIpsValid' => boolean true
private 'isForwardedValid' => boolean true
The actual field that would be rendered by your form is named get_post[phrase], not just phrase. Submitting get_post[phrase] in postman will work.
If you don't want that naming scheme you can override the getBlockPrefix method in your Form, eg:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class GetPostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('phrase', TextType::class);
}
public function getBlockPrefix()
{
return null;
}
}

Symfony Form Querybuilder with parameters

I have the need to create a dropdown field with grouped data:
My form:
class RetailerDetailFilterType extends AbstractType
{
public function getActiveRetailerMetrics(): array
{
return range(25,36);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('month', EntityType::class,
[
'class' => 'AppBundle:ConsolidatedOperatorCategoryLowData',
'query_builder' => function(ConsolidatedOperatorCategoryLowDataRepository $er){
return $er->getMinMaxByMetricQueryBuilder($this->getActiveRetailerMetrics());
}
]);
}
public function getBlockPrefix()
{
return 'key_metric';
}
}
My Repository:
class ConsolidatedOperatorCategoryLowDataRepository extends \Doctrine\ORM\EntityRepository
{
public function getMinMaxByMetricQueryBuilder($metricRange)
{
$qb = $this->getEntityManager()
->createQueryBuilder('d');
$qb
->select('d.id, YEAR(d.date) as dyear, MONTH(d.date) as dmonth')
->from('AppBundle:ConsolidatedOperatorCategoryLowData','d')
->where($qb->expr()->in('d.metric_id', $metricRange))
->groupBy('dyear')
->addGroupBy('dmonth')
->setMaxResults(10)
;
return $qb;
}
I'm getting
Warning: spl_object_hash() expects parameter 1 to be object, integer given
at UnitOfWork ->isScheduledForInsert (3182005)
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 710 +
at EntityManager ->contains (3182005)
in vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php at line 116 +
at IdReader ->getIdValue (3182005)
at call_user_func (array(object(IdReader), 'getIdValue'), 3182005)
in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php at line 205 +
at ArrayChoiceList ->flatten (array('id' => 3182005, 'dyear' => '2016', 'dmonth' => '12'), array(object(IdReader), 'getIdValue'), array(), array(), null)
in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php at line 200 +
at ArrayChoiceList ->flatten (array(array('id' => 3182005, 'dyear' => '2016', 'dmonth' => '12'), array('id' => 3186685, 'dyear' => '2017', 'dmonth' => '1'), array('id' => 3191365, 'dyear' => '2017', 'dmonth' => '2'), array('id' => 3195595, 'dyear' => '2017', 'dmonth' => '3'), array('id' => 3200275, 'dyear' => '2017', 'dmonth' => '4')), array(object(IdReader), 'getIdValue'), array(), array(), array(null))
in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php at line 91
I think it has to do with how Doctrine ORM deals with relationships. When you want to retrieve an entity by a referenced object's id, say find all posts by a user id, then you still have to pass the User-object to the QueryBuilder, not just the id. This is because Doctrine will resolve how those entities are connected itself.
It seems that metric_id actually is a reference to some kind of Metric-entity and just passing an array of int instead of the actual objects seems to trip up Doctrine's QueryBuilder.
You could try mapping the id's to a new instance of Metric and then pass that array instead.
Another solution - the one I would prefer - is to use Native SQL for this.

symfony, cant pass validation on a embedded form + file

Trying to embed a collection or just a simple type containing a file field, but both result in the same problem. On post the UploadedFile is present, but validation fails. "This value is not valid."
Bashing my head in all day, anyone?
Company form:
Simple embed with a file:
$builder->add('logo', new FileType(), [
'label' => 'Logo',
'required' => false,
'attr' => [
'accept' => 'image/*',
]
]);
Collection with files:
$builder->add('images', 'collection', array(
'type' => new FileType(),
'data' => [
new File(),
new File(),
new File(),
],
));
FileType:
class FileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$data = $builder->getData();
$builder->add('file', 'file', [
'label' => 'Bestand',
]);
$builder->add('submit', 'submit', [
'label' => 'Save',
]);
}
public function getName()
{
return 'file';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\File',
));
}
}
The entity has a functioniong one-to-many from Company to File.
Everything works, but when the form is submitted. Both fields have a "This value is not valid." error.
Here is a dump of the clientData preSubmit:
'logo' =>
object(Symfony\Component\HttpFoundation\File\UploadedFile)[13]
private 'test' => boolean false
private 'originalName' => string 'etc.jpg' (length=13)
private 'mimeType' => string 'image/jpeg' (length=10)
private 'size' => int 103843
private 'error' => int 0
'images' =>
array (size=3)
0 =>
object(Symfony\Component\HttpFoundation\File\UploadedFile)[14]
private 'test' => boolean false
private 'originalName' => string 'Screen Shot 2014-07-24 at 17.15.30.png' (length=38)
private 'mimeType' => string 'image/png' (length=9)
private 'size' => int 84102
private 'error' => int 0
1 => null
2 => null
Seems perfectly fine to me!

Zend DB query() - how do I perform a database update?

The last line of this code seems to return an object but does not perform the database query. What do I change to get this to actually perform the database update?
$sql = "UPDATE vi_admin_email SET processed_send_list = '?', status = '?' WHERE id = '?'";
$bind = array($addresses,$status,$id);
$res = $this->getAdapter()->query($sql,$bind);
Here is a var dump of the object in $res:
object(Zend_Db_Statement_Pdo)[102]
protected '_fetchMode' => int 2
protected '_stmt' =>
object(PDOStatement)[100]
public 'queryString' => string 'UPDATE vi_admin_email SET processed_send_list = '?', status = '?' WHERE id = '?'' (length=80)
protected '_adapter' =>
object(Zend_Db_Adapter_Pdo_Mysql)[43]
protected '_pdoType' => string 'mysql' (length=5)
protected '_numericDataTypes' =>
array
0 => int 0
1 => int 1
2 => int 2
'INT' => int 0
'INTEGER' => int 0
'MEDIUMINT' => int 0
'SMALLINT' => int 0
'TINYINT' => int 0
'BIGINT' => int 1
'SERIAL' => int 1
'DEC' => int 2
'DECIMAL' => int 2
'DOUBLE' => int 2
'DOUBLE PRECISION' => int 2
'FIXED' => int 2
'FLOAT' => int 2
protected '_defaultStmtClass' => string 'Zend_Db_Statement_Pdo' (length=21)
protected '_config' =>
array
'host' => string 'localhost' (length=9)
'username' => string 'root' (length=4)
'password' => string '' (length=0)
'dbname' => string 'vi' (length=2)
'charset' => null
'persistent' => boolean false
'options' =>
array
...
'driver_options' =>
array
...
protected '_fetchMode' => int 2
protected '_profiler' =>
object(Zend_Db_Profiler)[44]
protected '_queryProfiles' =>
array
...
protected '_enabled' => boolean false
protected '_filterElapsedSecs' => null
protected '_filterTypes' => null
protected '_defaultProfilerClass' => string 'Zend_Db_Profiler' (length=16)
protected '_connection' =>
object(PDO)[85]
protected '_caseFolding' => int 0
protected '_autoQuoteIdentifiers' => boolean true
protected '_allowSerialization' => boolean true
protected '_autoReconnectOnUnserialize' => boolean false
protected '_attribute' =>
array
empty
protected '_bindColumn' =>
array
empty
protected '_bindParam' =>
array
empty
protected '_sqlSplit' =>
array
0 => string 'UPDATE vi_admin_email SET processed_send_list = , status = WHERE id = ' (length=71)
protected '_sqlParam' =>
array
0 => string 'UPDATE vi_admin_email SET processed_send_list = , status = WHERE id = ' (length=71)
protected '_queryId' => null
To update an entry in Zend Framework you do as follow :
$data = array(
'processed_send_list' => $addresses,
'status' => $status
);
$dbAdapter = $this->getAdapter();
$where = $dbApdapter->quoteInto('id = ?', $id);
$this->update($data, $where);
With Zend_Db_Table, the data to be updated is specfied in an associative array as the first parameter. The data provided will be escaped when the query is executed. You also have to include a where statement as a string, like id = 1 as the second parameter. In the above example, the quoteInto is optionnal as you can write the where manually, but the value will not be escaped if you do it manually.
I thought I read "query" but you asked about "update".
So the statement would be:
$res->execute().
Forget what I wrote before.
Try this single line of code,
$res = $this->getAdapter()->query("UPDATE vi_admin_email SET processed_send_list = '$addresses', status = '$status' WHERE id = '$id'");