有一个表,其中显示来自 2 个表的数据,如何向该表添加另一个字段,该字段将根据其他 2 个表或一个表的条件包含信息,例如,我需要添加一个字段Trial
,其中是基于字段的信息Price
,如果字段Price
大于 0,则Trial
插入一个值,如果为 0,则插入另一个值。我以这种方式向方法中configureListFields
添加了一个字段Trial
:->add('trial')
它已添加,现在如何用数据填充它,这是整个代码:
namespace AdminBundle\Admin;
use IntlDateFormatter;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
class PurchaseAdmin extends AbstractAdmin
{
protected $datagridValues = [
'_page' => 1,
'_sort_order' => 'DESC',
'_sort_by' => 'createdAt',
];
protected $accessMapping = [
'refund' => 'REFUND',
];
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$rootAlias = $query->getRootAliases()[0];
$query->leftJoin($rootAlias.'.paymentTransaction', 'pt');
$query->leftJoin($rootAlias.'.language', 'll');
$query->addSelect('pt, ll');
return $query;
}
public function configureRoutes(RouteCollection $collection)
{
parent::configureRoutes($collection);
$collection->remove('delete');
$collection->remove('create');
$collection->remove('edit');
$collection->add('refund', $this->getRouterIdParameter().'/refund');
$collection->add('studentInfo', $this->getRouterIdParameter().'/student-info');
}
/**
* @param DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add(
'teacher', null, [
'show_filter' => true
]
)
->add(
'student', null, [
'show_filter' => true
]
)
->add(
'language', null, [
'show_filter' => true
]
)
->add('lessonLength')
->add('lessons')
->add(
'status', null, [], 'choice', [
'choices' => [
0 => 'created',
1 => 'paid',
2 => 'used',
3 => 'refunded',
],
]
);
}
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier(
'id',
null,
[
'route' => [
'name' => 'show'
]
]
)
->add('statusText')
->add('teacher')
->add('student')
->add(
'studentInfo',
'string',
[
'template' => ':CRUD:list__action_student_info.html.twig'
]
)
->add('language')
->add('lessonLength')
->add('lessons')
->add('lessonsLeft')
->add('price')
->add('trial')
->add('teacher.currency', null, ['label' => 'Currency'])
->add('paymentTransaction')
->add(
'createdAt', 'datetime', [
'dateType' => IntlDateFormatter::SHORT,
'timeType' => IntlDateFormatter::SHORT,
]
)
->add(
'updatedAt', 'datetime', [
'dateType' => IntlDateFormatter::SHORT,
'timeType' => IntlDateFormatter::SHORT,
]
)
->add(
'_action',
null,
[
'actions' => [
'show' => [],
'refund' => [
'template' => ':CRUD:list__action_refund.html.twig'
],
]
]
);
}
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('id')
->add('price')
->add('lessonLength')
->add('lessons')
->add('lessonsLeft')
->add('commission')
->add('status')
->add('createdAt')
->add('updatedAt');
}
/**
* @param ShowMapper $showMapper
*/
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('teacher')
->add('student')
->add('language')
->add('price')
->add('teacher.currency', null, ['label' => 'Currency'])
->add('lessonLength')
->add('lessons')
->add('lessonsLeft')
->add('commission')
->add('statusText')
->add('paymentTransaction')
->add('createdAt')
->add('updatedAt');
}
public function getTemplate($name)
{
if ($name === 'refund') {
return ':Purchase:base_refund.html.twig';
}
return parent::getTemplate($name);
}
}