我是Symfony和OOP的新手。
尝试创建一个用户可以上传其活动文件的应用。每个用户都有一个或多个活动。每个活动都只有一个文件。
用户只会上传文件,并且我想要创建一个新的Activity();
所以我认为Controller应该是这样的:
详细文件可在此处找到:https://gist.github.com/carambazza/53804469d3021601f1bb
// DashboardController.php
class DashboardController extends Controller
{
public function indexAction(Request $request)
{
$activity = new Activity();
$form = $this->createForm(ActivityType::class, $activity);
$form->handleRequest($request);
// form is valid ...
}
// ActivityType.php
class ActivityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('description', TextType::class)
->add(
'activityFile',
CollectionType::class,
array(
'entry_type' => ActivityFileType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
)
)
->add('save', SubmitType::class, array('label' => 'Upload .fit file'));
}
// ActivityFileType.php
class ActivityFileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', FileType::class,
array(
"required" => TRUE,
"attr" => array(
//"accept" => "image/*",
"multiple" => true,
)
)
);
}
映射是这样的:
// Activity.orm.xml
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity repository-class="ChartsBundle\Repository\ActivityRepository" name="ChartsBundle\Entity\Activity">
<id name="id" type="guid" column="id">
<generator strategy="UUID"/>
</id>
<many-to-one field="user" target-entity="User" inversed-by="activities">
<join-column name="user_id" referenced-column-name="id" />
</many-to-one>
<one-to-one field="activityFile" target-entity="ActivityFile">
<join-column name="activity_file_id" referenced-column-name="id" />
</one-to-one>
<!-- other connnections -->
<!-- all fields -->
</entity>
</doctrine-mapping>
//ActivityFile.orm.xml
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity repository-class="ChartsBundle\Repository\ActivityFile" name="ChartsBundle\Entity\ActivityFile">
<id name="id" type="guid" column="id">
<generator strategy="UUID"/>
</id>
<!-- all fields -->
</entity>
</doctrine-mapping>
问题:
答案 0 :(得分:1)
对我来说,表格不对。集合类型用于一对多关系,例如,如果您想在编辑用户时向用户内联添加活动。对于一对一的关系,您希望在活动表单中嵌入活动文件的表单,因此您只需要这样而不是集合类型:
$scope.data
你们的关系看起来很正确。建议您的ActivityFile实体的存储库文件名不一致,应该是var myObject = { value: 1 };
var $scope = {};
$scope.data = myObject.value;
$scope.data = 3;
// This should alert the value of 1
window.alert(myObject.value);
,但这是一个小问题。