Symfony Admin generator - show Doctrine Collection - php

In my model i have:
* #method Doctrine_Collection getComments() Returns the current record's "Comments" collection
Default if i generated admin then this isn't showing in list.
If is in generator.yml :
config:
actions: ~
fields: ~
list:
display: [id, title, comments]
filter: ~
form: ~
edit: ~
new: ~
Then this show me
<pre> Doctrine_Collection data : Array( ) </pre>
instead of list of comments.
I know - i can get files from cache and showing this, but maybe this is possible only with generator.yml ?
For example if i have relation one to many then this showing me this name.
I dont want use cache for this!
thanks!

you can use a function for your problem.
For example, in my generator.yml
list:
display: [id, description, public, nbQuestions]
nbQuestions is a function in Object.class.php
public function getNbQuestions() {
return $this->getQuestion()->count();
}
The admin generator will automatically call the "getYouField" Method in the object class. So you can describe a function which return a long string for you doctrine collection.

There is an other way than only displaying a count.
You can add a partial in your generator.yml:
list:
display: [id, description, public, _comments]
Then in your partial (_comments.php), you can call the relation and display what ever you want (add style, other infos, etc ..):
<?php
// note that you will need to change the $object
echo $object->getComments()->count();
?>
In an other way, it could be usefull to have all comments listed in the edit view. In your generator.yml:
form:
# don't forget to add others fields
display: [_comments]
And then in your partial:
<ul>
<?php foreach($form->getObject()->getComments() as $comment): ?>
<li><?php echo $comment->getBody() ?></li>
<?php endforeach; ?>
</ul>
And if you want to combine both in the same partial (don't forget to rename $object):
<?php if(isset($form)): ?>
<ul>
<?php foreach($form->getObject()->getComments() as $comment): ?>
<li><?php echo $comment->getBody() ?></li>
<?php endforeach; ?>
</ul>
<?php elseif(isset($object)): ?>
<?php
// note that you will need to change the $object
echo $object->getComments()->count();
?>
<?php endif; ?>

Related

Laravel component is not receiving variables, no matter what i do

First, i have the component file, located at resources/views/component.
game-card.blade.php
#props(['game'])
<div class = 'games'>
<a href = 'game/{{$game->id}}'> view page </a>
<p> game: {{$game->name}} </p> <br>
<p> genre: {{$game->genre}} </p> <br>
</div>
Then this component is called at my view, located in resources/views
listing.blade.php
#extends('layout')
#section('list')
<div class = 'listContainer'>
#unless(count($games) === 0)
#foreach($games as $game)
//doesn't work
<x-game-card :game = "$game"/>
#endforeach
#else
<p> 0 Games </p>
#endunless
</div>
#endsection
The variable $game is not passed in the component <x-game-card/>, i even tried to use short atribute syntax (<x-game-card :$game/>) but it still doesn't work.
If it matters, the file listing.blade.php is yielded at the file layout.blade.php, located in the same folder.
layout.blade.php
<body>
<!-- Header -->
#yield('list')
#yield('singlegame')
#include('partials._footer')
For any prop that you want to pass to your component, you need to register it on the component class. In this case, the class is probably app/View/Components/GameCard.php
On the class, you need to do something like:
class GameCard extends Component
{
public $game;
public function __construct($game)
{
$this->game = $game;
}
Source: https://laravel.com/docs/9.x/blade#passing-data-to-components
i found the root of the problem. I just can't believe that the solution is so simple. I am going to post it here so less people make the same mistake i did
First, create the class (i personally use the command php artisan make:component to do that) and update it just like Nico did.
Second, when you put the <x-component in the HTML, MAKE SURE TO NOT LEAVE ANY SPACES IN THE VARIABLE!!!
my mistake was using <x-game-card :game = "$game"/>
instead of <x-game-card :game="$game"/>

October CMS backend list's column get array key value

Hi I'm new to October cms . I have defined the below shown method in my model class. The method is also used to show the select options in the backend form. This method returns an array where the key is a value similar to a field value in db. I have defined the method as static because it is also recommended in the front end where I get the function and process it with db record and iterate it to show the value of the array that matches the key. It all works fine. Thing is in my columns.yaml file, how do I list the method's array value which matches the db record as I did in front end.
public static function getSampleOptions()
{
return[
'1'=>'Sample1',
'2'=>'Sample2'
];
}
Hello friends I found the answer with some help from October CMS Help/Support
http://octobercms.com/index.php/forum/post/dropdown-shows-its-value-than-key-name-in-list-controller
and referred few concepts of laravel.
Model Class Method
public static function getSampleOptions()
{
return[
'1'=>'Mobile App',
'2'=>'Web App'
];
}
Columns.Yaml file
sample:
label: Sample Column
type: dropdown
Again back in the model, declare the attributes object and include the filed name as key with empty value
public $attributes = ['sample'=>''];
Define the getfield_nameAttribute() function to set the associated value for the appropriate key in the column
public function getSampleAttribute()
{
$result = $this->attributes['sample'];
$options = $this->getSampleOptions();
foreach($options as $key=>$value)
{
if($key == $result)
{
return $value;
}
}
}
Updated
The solution to rectify the problem while editing the record is simple.
Create a partial and modify the fields. yaml
_sample_options.htm (partial) // file name should begin with_(underscore)
<?php
$fieldOptions = $model->getSampleOptions();
$sample = $model->attributes['sample'];
?>
<select id="<?= $field->getId() ?>" name="<?= $field->getName() ?>" class="form-control custom-select" <?= $field->getAttributes() ?>>
<?php foreach($fieldOptions as $key=>$label)
{
?>
<option value="<?= $key ?>" <?php echo ($sample == $key)?"selected":''; ?>><?= $label ?></option>
<?php
} ?>
</select>
Here the $model and $field are the partial variables that are used to access the intended model's methods and properties.
Documentation : https://octobercms.com/docs/backend/forms#field-partial
Fields.Yaml file
sample:
label: Sample Field
type: partial
path: $/october/demo/controllers/sample/_sample_options.htm //path where the partial is located in the controller view
break a for loop in october cms template if condition not satisfied?
{% for key, pTest in pack.products %}
{{loop.length}}
<li>{{pTest.productable.name}} {{ key }}</li>
{% if key == 2 %}
<li class="more">...</li>
{% endif %}
{% endfor %}

how to link dynamic dropdown to different pages

This is the dynamic link from category table join to news table so i want to link in <li> to different page like allnational, allsports (these are the things in dropdown) function in home controller:
<ul class="dropdown-menu">
<?php if($category){
foreach($category as $c){
?>
<li><?php echo $c['category_name'];?></li>
<?php }} ?>
</ul>
just want to add link in above href
table scheme:
category_id: 1 , 2 , 3
category_name: national , international , sports
"category_id & category_name are column and its value listed above respectively"
Home controller:
public function allnational(){
$data['allnat'] = $this->newsmodel->getAllNational();
$this->load->view('allnational',$data);
}
public function allInternational(){
$data['allint'] = $this->newsmodel->getAllInternational();
$this->load->view('allinternational',$data);
}
if you can give suggestion what will be the model as well.
Note: model is already loaded.

What is the code inside a paginator view?(Laravel)

<?php echo $users->links('view.name'); ?>
If I specify this view in laravel-4, what will be the code inside it? I can't find an example in the docs. Any example please?
according to this http://laravel.com/docs/pagination
If you use the paginator like this
<?php echo $users->links(); ?>
Laravel will use the default view which is defined inside app/config/view.php as
'pagination' => 'pagination::slider-3',
With this default option, your pagination will use the Illuminate/Pagination/views/slider-3.php view,
which is the following:
<?php if ($paginator->getLastPage() > 1): ?>
<ul class="pagination">
<?php echo $presenter->render(); ?>
</ul>
<?php endif; ?>
You can define your own view, and then use inside it the $paginator object to format it according to your needs.
You can see an example here.

Is there a way to have a navigation entry with no link in zend navigation?

I'm sure my question is pretty straight forward, and I've been looking for an answer to this, but I can't seem to make it work. I want to do something like this:
<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<dashboard>
<label>Dashboard</label>
<controller>dashboard</controller>
<action>index</action>
<module>global</module>
</dashboard>
<bills>
<label>Bills</label>
<pages>
<create-bill>
<label>Create New Bill</label>
<controller>bill</controller>
<action>create</action>
<module>global</module>
</create-bill>
</pages>
</bills>
</configdata>
Please note that in the <bills> section, I want to have a category with just a label, that way when I add styling later, I can hover over "Bills" and "Create New Bill" and other links will be shown, but clicking on "Bills" shouldn't do anything because it's just a category header.
I hope that makes sense.
You must specify a type for your page otherwise Zend_Navigation will throw an exception. In cases like yours I always use a Zend_Navigation_Page_Uri as page type and specify its uri to #. To apply this to your config file you could do this
<bills>
<label>Bills</label>
<uri>#</uri>
<pages>
<create-bill>
<label>Create New Bill</label>
<controller>bill</controller>
<action>create</action>
<module>global</module>
</create-bill>
</pages>
</bills>
The generated markup still contains a link but it will not point anywhere.
Moreover, since you need to bind some javascript to it in order to show the menu, you could even disable it by returning false in the click handler for that links.
In order to attach javascript callbacks (or some css) to that kind of link you may find useful to attach a class to those links. Within the same configuration file, you could with this code
<bills>
<label>Bills</label>
<uri>#</uri>
<class>fakelink</class>
<pages>
<create-bill>
<label>Create New Bill</label>
<controller>bill</controller>
<action>create</action>
<module>global</module>
</create-bill>
</pages>
</bills>
In this case the generated markup would be
<li class="fakelink>
Bills
<ul>submenu here</ul>
</li>
and you could easily select that kind of links with a javascript library. For example with jQuery you could do this:
$(function() { $('.fakelinks > a').click(function () { return false; }); });
There is actually another way to solve this.
You can set custom properties on all Zend_Navigation_Page just by defining extra configuration options in your xml/array/...
Then by using a dedicated partial to render your menu/breadcrumbs you can perfectly skip rendering the <a> tag or render a completely different markup based on these properties.
<!-- ... -->
<page1>
<label>Page 1</label>
<uri>page1</uri>
<link>false</link> <!-- Custom property -->
<pages>
<page1_1>
<label>Page 1.1</label>
<uri>page1/page1_1</uri>
</page1_1>
<page1_2>
<label>Page 1.2</label>
<uri>page1/page1_2</uri>
</page1_2>
<page1_3>
<label>Page 1.3</label>
<uri>page1/page1_3</uri>
</page1_3>
</pages>
</page1>
<!-- ... -->
Note: I'm using the breadcrumbs example here but most of the Navigation View_Helpers have a setPartial() method, including the menu helper.
Then in your view script or layout you just specify that your breadcrumbs helper needs to use a partial.
<?php
echo $this->navigation()->breadcrumbs()->setPartial('my_breadcrumbs.phtml');
And in your partial you loop over the pages in year breadcrumb trail and check the custom properties for each page.
<?php
foreach($this->pages as $page)
{
$properties = $page->getCustomProperties();
// Check if we need to render the link tag
if($properties['link'] !== false){
echo '<a href="' . $page->getHref() . '">';
}
// Render the label
echo $page->getLabel();
// And check if we need to render the closing tag
if($properties['link'] !== false){
echo '</a>';
}
}
Note: By using a partial you'll lose some default functionality of the Breadcrumb View_Helper like setLinkLast, setSeparator, ... but these shouldn't pose too much of a problem.
If you're happy for your category labels to be spans then just specifying an empty URI will do the job. Zend_View_Helper_Navigation_Menu::htmlify() is what renders a Zend_Navigation_Page:
/**
* Returns an HTML string containing an 'a' element for the given page if
* the page's href is not empty, and a 'span' element if it is empty
*
* Overrides {#link Zend_View_Helper_Navigation_Abstract::htmlify()}.
*
* #param Zend_Navigation_Page $page page to generate HTML for
* #return string HTML string for the given page
*/
public function htmlify(Zend_Navigation_Page $page)
Example output:
<ul class="navigation">
<li>
<span id="menu-staff">Staff</span>
<ul>
<li>
Book Holiday
</li>
<li>
View Booked and Remaining Holiday
</li>
</ul>
</li>
</ul>
I went about it in a different way:
nav:
User:
label: Account Services
uri: #fakeUri
Then in my template code:
<?php foreach ($navSettings as $navSetting): ?>
<?php if ('#fakeUri' === $navSetting->getUri()): ?>
<?php echo $navSetting->getLabel() ?>
<?php else: ?>
<a href="<?php echo $navSetting->getUri() ?>"><?php echo $navSetting->getLabel()) ?>
<?php endif ?>
<?php endforeach ?>
<page_bills>
<label>Bills</label>
<type>uri</type>
<pages>
</pages>
</page_bills>

Categories